Changeset 01e48c1 in mainline for src/lib


Ignore:
Timestamp:
2005-09-15T21:02:27Z (20 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0a50f59
Parents:
d6dcdd2e
Message:

Optimize sorting functions so that malloc() and free() is called only for e_size > EBUFSIZE.
Smaller buffers are allocated directly on the stack.

Some copyright holder fixes on some files written by Ondrej Palkovsky.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lib/sort.c

    rd6dcdd2e r01e48c1  
    3232#include <panic.h>
    3333
     34#define EBUFSIZE        32
     35
    3436static void _qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b), void * pivot, void * tmp);
    3537
     
    3840 */
    3941void qsort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) {
    40         void * tmp = (void *) malloc(e_size);
    41         void * pivot = (void *) malloc(e_size);
     42        __u8 buf_tmp[EBUFSIZE];
     43        __u8 buf_pivot[EBUFSIZE];
     44        void * tmp = buf_tmp;
     45        void * pivot = buf_pivot;
     46
     47        if (e_size > EBUFSIZE) {
     48                pivot = (void *) malloc(e_size);
     49                tmp = (void *) malloc(e_size);
    4250       
    43         if (!tmp || !pivot) {
    44                 panic("Cannot allocate memory\n");
     51                if (!tmp || !pivot) {
     52                        panic("Cannot allocate memory\n");
     53                }
    4554        }
    4655
    4756        _qsort(data, n, e_size, cmp, pivot, tmp);
    4857       
    49         free(tmp);
    50         free(pivot);
     58        if (e_size > EBUFSIZE) {
     59                free(tmp);
     60                free(pivot);
     61        }
    5162}
    5263
    5364
    5465void bubblesort(void * data, count_t n, size_t e_size, int (* cmp) (void * a, void * b)) {
     66        __u8 buf_slot[EBUFSIZE];
    5567        bool done = false;
    5668        void * p;
    57         void * slot = (void *) malloc(e_size);
     69        void * slot = buf_slot;
     70       
     71        if (e_size > EBUFSIZE) {
     72                slot = (void *) malloc(e_size);
     73               
     74                if (!slot) {
     75                        panic("Cannot allocate memory\n");
     76                }
     77        }
    5878
    5979        while (!done) {
     
    6989        }
    7090       
    71         free(slot);
     91        if (e_size > EBUFSIZE) {       
     92                free(slot);
     93        }
    7294}
    7395
Note: See TracChangeset for help on using the changeset viewer.