Changeset f2460a50 in mainline
- Timestamp:
- 2017-05-30T06:16:25Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c726209
- Parents:
- 719a208
- Files:
-
- 3 added
- 11 edited
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
kernel/Makefile
r719a208 rf2460a50 242 242 generic/src/lib/memstr.c \ 243 243 generic/src/lib/memfnc.c \ 244 generic/src/lib/ sort.c \244 generic/src/lib/gsort.c \ 245 245 generic/src/lib/str.c \ 246 246 generic/src/lib/elf.c \ -
kernel/genarch/src/acpi/madt.c
r719a208 rf2460a50 46 46 #include <mm/slab.h> 47 47 #include <memstr.h> 48 #include < sort.h>48 #include <gsort.h> 49 49 50 50 struct acpi_madt *acpi_madt = NULL; -
kernel/generic/include/gsort.h
r719a208 rf2460a50 33 33 */ 34 34 35 #ifndef KERN_ SORT_H_36 #define KERN_ SORT_H_35 #ifndef KERN_GSORT_H_ 36 #define KERN_GSORT_H_ 37 37 38 38 #include <typedefs.h> … … 41 41 42 42 extern bool gsort(void *, size_t, size_t, sort_cmp_t, void *); 43 extern bool qsort(void *, size_t, size_t, sort_cmp_t, void *);44 43 45 44 #endif -
kernel/generic/src/lib/gsort.c
r719a208 rf2460a50 27 27 */ 28 28 29 /** @addtogroup generic29 /** @addtogroup libc 30 30 * @{ 31 31 */ … … 40 40 */ 41 41 42 #include <gsort.h> 43 #include <memstr.h> 42 44 #include <mm/slab.h> 43 #include <memstr.h>44 #include <sort.h>45 45 46 46 /** Immediate buffer size. … … 77 77 78 78 while (i < cnt) { 79 if ((i >0) &&79 if ((i != 0) && 80 80 (cmp(INDEX(data, i, elem_size), 81 81 INDEX(data, i - 1, elem_size), arg) == -1)) { … … 90 90 } 91 91 92 /** Quicksort93 *94 * Apply generic quicksort algorithm on supplied data,95 * using pre-allocated buffers.96 *97 * @param data Pointer to data to be sorted.98 * @param cnt Number of elements to be sorted.99 * @param elem_size Size of one element.100 * @param cmp Comparator function.101 * @param arg 3rd argument passed to cmp.102 * @param slot Pointer to scratch memory buffer103 * elem_size bytes long.104 * @param pivot Pointer to scratch memory buffer105 * elem_size bytes long.106 *107 */108 static void _qsort(void *data, size_t cnt, size_t elem_size, sort_cmp_t cmp,109 void *arg, void *slot, void *pivot)110 {111 if (cnt > 4) {112 size_t i = 0;113 size_t j = cnt - 1;114 115 memcpy(pivot, data, elem_size);116 117 while (true) {118 while ((cmp(INDEX(data, i, elem_size), pivot, arg) < 0) && (i < cnt))119 i++;120 121 while ((cmp(INDEX(data, j, elem_size), pivot, arg) >= 0) && (j > 0))122 j--;123 124 if (i < j) {125 memcpy(slot, INDEX(data, i, elem_size), elem_size);126 memcpy(INDEX(data, i, elem_size), INDEX(data, j, elem_size),127 elem_size);128 memcpy(INDEX(data, j, elem_size), slot, elem_size);129 } else130 break;131 }132 133 _qsort(data, j + 1, elem_size, cmp, arg, slot, pivot);134 _qsort(INDEX(data, j + 1, elem_size), cnt - j - 1, elem_size,135 cmp, arg, slot, pivot);136 } else137 _gsort(data, cnt, elem_size, cmp, arg, slot);138 }139 140 92 /** Gnome sort wrapper 141 93 * … … 143 95 * allocations for storing the slot element for generic 144 96 * gnome sort algorithm. 145 *146 * This function can sleep.147 97 * 148 98 * @param data Pointer to data to be sorted. … … 175 125 } 176 126 177 /** Quicksort wrapper178 *179 * This is only a wrapper that takes care of memory180 * allocations for storing the pivot and temporary elements181 * for generic quicksort algorithm.182 *183 * This function can sleep.184 *185 * @param data Pointer to data to be sorted.186 * @param cnt Number of elements to be sorted.187 * @param elem_size Size of one element.188 * @param cmp Comparator function.189 * @param arg 3rd argument passed to cmp.190 *191 * @return True if sorting succeeded.192 *193 */194 bool qsort(void *data, size_t cnt, size_t elem_size, sort_cmp_t cmp, void *arg)195 {196 uint8_t ibuf_slot[IBUF_SIZE];197 uint8_t ibuf_pivot[IBUF_SIZE];198 void *slot;199 void *pivot;200 201 if (elem_size > IBUF_SIZE) {202 slot = (void *) malloc(elem_size, 0);203 if (!slot)204 return false;205 206 pivot = (void *) malloc(elem_size, 0);207 if (!pivot) {208 free(slot);209 return false;210 }211 } else {212 slot = (void *) ibuf_slot;213 pivot = (void *) ibuf_pivot;214 }215 216 _qsort(data, cnt, elem_size, cmp, arg, slot, pivot);217 218 if (elem_size > IBUF_SIZE) {219 free(pivot);220 free(slot);221 }222 223 return true;224 }225 226 127 /** @} 227 128 */ -
uspace/app/bdsh/cmds/modules/ls/ls.c
r719a208 rf2460a50 39 39 #include <vfs/vfs.h> 40 40 #include <str.h> 41 #include <sort.h>42 41 43 42 #include "ls.h" … … 62 61 static unsigned int ls_start(ls_job_t *); 63 62 static void ls_print(struct dir_elem_t *); 64 static int ls_cmp( void *, void *,void *);63 static int ls_cmp(const void *, const void *); 65 64 static signed int ls_scan_dir(const char *, DIR *, struct dir_elem_t **); 66 65 static unsigned int ls_recursive(const char *, DIR *); … … 104 103 * @param a Pointer to the structure of the first element. 105 104 * @param b Pointer to the structure of the second element. 106 * @param arg Pointer for an other and optionnal argument.107 105 * 108 106 * @return -1 if a < b, 1 otherwise. 109 107 */ 110 static int ls_cmp( void *a, void *b, void *arg)111 { 112 struct dir_elem_t *da = a;113 struct dir_elem_t *db = b;108 static int ls_cmp(const void *a, const void *b) 109 { 110 struct dir_elem_t const *da = a; 111 struct dir_elem_t const *db = b; 114 112 115 113 if ((da->s.is_directory && db->s.is_file) || … … 191 189 } 192 190 193 if (ls.sort) { 194 if (!qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), 195 ls_cmp, NULL)) { 196 printf("Sorting error.\n"); 197 } 198 } 191 if (ls.sort) 192 qsort(&tosort[0], nbdirs, sizeof(struct dir_elem_t), ls_cmp); 199 193 200 194 for (i = 0; i < nbdirs; i++) -
uspace/app/top/top.c
r719a208 rf2460a50 42 42 #include <sys/time.h> 43 43 #include <errno.h> 44 #include < sort.h>44 #include <gsort.h> 45 45 #include "screen.h" 46 46 #include "top.h" -
uspace/dist/src/c/demos/top/top.c
r719a208 rf2460a50 42 42 #include <sys/time.h> 43 43 #include <errno.h> 44 #include < sort.h>44 #include <gsort.h> 45 45 #include "screen.h" 46 46 #include "top.h" -
uspace/lib/c/Makefile
r719a208 rf2460a50 81 81 generic/event.c \ 82 82 generic/errno.c \ 83 generic/gsort.c \ 83 84 generic/loc.c \ 84 85 generic/mem.c \ … … 158 159 generic/stacktrace.c \ 159 160 generic/arg_parse.c \ 160 generic/sort.c \161 161 generic/stats.c \ 162 162 generic/assert.c \ 163 163 generic/pio_trace.c \ 164 generic/qsort.c \ 164 165 generic/uuid.c \ 165 166 generic/vbd.c \ … … 181 182 test/main.c \ 182 183 test/odict.c \ 184 test/qsort.c \ 183 185 test/sprintf.c \ 184 186 test/str.c -
uspace/lib/c/generic/gsort.c
r719a208 rf2460a50 40 40 */ 41 41 42 #include < sort.h>42 #include <gsort.h> 43 43 #include <mem.h> 44 44 #include <malloc.h> … … 90 90 } 91 91 92 /** Quicksort93 *94 * Apply generic quicksort algorithm on supplied data,95 * using pre-allocated buffers.96 *97 * @param data Pointer to data to be sorted.98 * @param cnt Number of elements to be sorted.99 * @param elem_size Size of one element.100 * @param cmp Comparator function.101 * @param arg 3rd argument passed to cmp.102 * @param slot Pointer to scratch memory buffer103 * elem_size bytes long.104 * @param pivot Pointer to scratch memory buffer105 * elem_size bytes long.106 *107 */108 static void _qsort(void *data, size_t cnt, size_t elem_size, sort_cmp_t cmp,109 void *arg, void *slot, void *pivot)110 {111 if (cnt > 4) {112 size_t i = 0;113 size_t j = cnt - 1;114 115 memcpy(pivot, data, elem_size);116 117 while (true) {118 while ((cmp(INDEX(data, i, elem_size), pivot, arg) < 0) && (i < cnt))119 i++;120 121 while ((cmp(INDEX(data, j, elem_size), pivot, arg) >= 0) && (j > 0))122 j--;123 124 if (i < j) {125 memcpy(slot, INDEX(data, i, elem_size), elem_size);126 memcpy(INDEX(data, i, elem_size), INDEX(data, j, elem_size),127 elem_size);128 memcpy(INDEX(data, j, elem_size), slot, elem_size);129 } else130 break;131 }132 133 _qsort(data, j + 1, elem_size, cmp, arg, slot, pivot);134 _qsort(INDEX(data, j + 1, elem_size), cnt - j - 1, elem_size,135 cmp, arg, slot, pivot);136 } else137 _gsort(data, cnt, elem_size, cmp, arg, slot);138 }139 140 92 /** Gnome sort wrapper 141 93 * … … 173 125 } 174 126 175 /** Quicksort wrapper176 *177 * This is only a wrapper that takes care of memory178 * allocations for storing the pivot and temporary elements179 * for generic quicksort algorithm.180 *181 * @param data Pointer to data to be sorted.182 * @param cnt Number of elements to be sorted.183 * @param elem_size Size of one element.184 * @param cmp Comparator function.185 * @param arg 3rd argument passed to cmp.186 *187 * @return True if sorting succeeded.188 *189 */190 bool qsort(void *data, size_t cnt, size_t elem_size, sort_cmp_t cmp, void *arg)191 {192 uint8_t ibuf_slot[IBUF_SIZE];193 uint8_t ibuf_pivot[IBUF_SIZE];194 void *slot;195 void *pivot;196 197 if (elem_size > IBUF_SIZE) {198 slot = (void *) malloc(elem_size);199 if (!slot)200 return false;201 202 pivot = (void *) malloc(elem_size);203 if (!pivot) {204 free(slot);205 return false;206 }207 } else {208 slot = (void *) ibuf_slot;209 pivot = (void *) ibuf_pivot;210 }211 212 _qsort(data, cnt, elem_size, cmp, arg, slot, pivot);213 214 if (elem_size > IBUF_SIZE) {215 free(pivot);216 free(slot);217 }218 219 return true;220 }221 222 127 /** @} 223 128 */ -
uspace/lib/c/include/gsort.h
r719a208 rf2460a50 42 42 43 43 extern bool gsort(void *, size_t, size_t, sort_cmp_t, void *); 44 extern bool qsort(void *, size_t, size_t, sort_cmp_t, void *);45 44 46 45 #endif -
uspace/lib/c/include/stdlib.h
r719a208 rf2460a50 37 37 38 38 #include <malloc.h> 39 #include <qsort.h> 39 40 #include <stacktrace.h> 40 41 -
uspace/lib/c/test/main.c
r719a208 rf2460a50 33 33 34 34 PCUT_IMPORT(odict); 35 PCUT_IMPORT(qsort); 35 36 PCUT_IMPORT(sprintf); 36 37 PCUT_IMPORT(str); -
uspace/lib/clui/tinput.c
r719a208 rf2460a50 27 27 */ 28 28 29 #include <sort.h>30 29 #include <stdio.h> 31 30 #include <stdlib.h> … … 583 582 584 583 /** Compare two entries in array of completions. */ 585 static int compl_cmp( void *va, void *vb, void *arg)584 static int compl_cmp(const void *va, const void *vb) 586 585 { 587 586 const char *a = *(const char **) va; … … 741 740 /* No common prefix. Sort and display all entries. */ 742 741 743 qsort(compl, cnum, sizeof(char *), compl_cmp , NULL);742 qsort(compl, cnum, sizeof(char *), compl_cmp); 744 743 745 744 tinput_jump_after(ti); -
uspace/lib/ext4/src/directory_index.c
r719a208 rf2460a50 38 38 #include <errno.h> 39 39 #include <mem.h> 40 #include <sort.h>41 40 #include <stdlib.h> 42 41 #include <str.h> … … 668 667 * @param arg1 First entry 669 668 * @param arg2 Second entry 670 * @param dummy Unused parameter, can be NULL671 669 * 672 670 * @return Classic compare result … … 674 672 * 675 673 */ 676 static int ext4_directory_dx_entry_comparator(void *arg1, void *arg2, void *dummy) 677 { 678 ext4_dx_sort_entry_t *entry1 = arg1; 679 ext4_dx_sort_entry_t *entry2 = arg2; 674 static int ext4_directory_dx_entry_comparator(const void *arg1, 675 const void *arg2) 676 { 677 ext4_dx_sort_entry_t const *entry1 = arg1; 678 ext4_dx_sort_entry_t const *entry2 = arg2; 680 679 681 680 if (entry1->hash == entry2->hash) … … 793 792 /* Sort all entries */ 794 793 qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), 795 ext4_directory_dx_entry_comparator , NULL);794 ext4_directory_dx_entry_comparator); 796 795 797 796 /* Allocate new block for store the second part of entries */ -
uspace/lib/posix/source/stdlib.c
r719a208 rf2460a50 47 47 #include "posix/unistd.h" 48 48 49 #include "libc/ sort.h"49 #include "libc/qsort.h" 50 50 #include "libc/str.h" 51 51 #include "libc/vfs/vfs.h" … … 136 136 137 137 /** 138 * Private helper function that serves as a compare function for qsort().139 *140 * @param elem1 First element to compare.141 * @param elem2 Second element to compare.142 * @param compare Comparison function without userdata parameter.143 * @return Relative ordering of the elements.144 */145 static int sort_compare_wrapper(void *elem1, void *elem2, void *userdata)146 {147 int (*compare)(const void *, const void *) = userdata;148 int ret = compare(elem1, elem2);149 150 /* Native qsort internals expect this. */151 if (ret < 0) {152 return -1;153 } else if (ret > 0) {154 return 1;155 } else {156 return 0;157 }158 }159 160 /**161 138 * Array sorting utilizing the quicksort algorithm. 162 139 * … … 169 146 int (*compare)(const void *, const void *)) 170 147 { 171 /* Implemented in libc with one extra argument. */ 172 qsort(array, count, size, sort_compare_wrapper, compare); 148 qsort(array, count, size, compare); 173 149 } 174 150
Note:
See TracChangeset
for help on using the changeset viewer.