Home | History | Annotate | Download | only in libcutils
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdlib.h>
     18 #include <cutils/qsort_r_compat.h>
     19 
     20 #if HAVE_BSD_QSORT_R
     21 
     22 /*
     23  * BSD qsort_r parameter order is as we have defined here.
     24  */
     25 
     26 void qsort_r_compat(void* base, size_t nel, size_t width, void* thunk,
     27         int (*compar)(void*, const void* , const void*)) {
     28     qsort_r(base, nel, width, thunk, compar);
     29 }
     30 
     31 #elif HAVE_GNU_QSORT_R
     32 
     33 /*
     34  * GNU qsort_r parameter order places the thunk parameter last.
     35  */
     36 
     37 struct compar_data {
     38     void* thunk;
     39     int (*compar)(void*, const void* , const void*);
     40 };
     41 
     42 static int compar_wrapper(const void* a, const void* b, void* data) {
     43     struct compar_data* compar_data = (struct compar_data*)data;
     44     return compar_data->compar(compar_data->thunk, a, b);
     45 }
     46 
     47 void qsort_r_compat(void* base, size_t nel, size_t width, void* thunk,
     48         int (*compar)(void*, const void* , const void*)) {
     49     struct compar_data compar_data;
     50     compar_data.thunk = thunk;
     51     compar_data.compar = compar;
     52     qsort_r(base, nel, width, compar_wrapper, &compar_data);
     53 }
     54 
     55 #else
     56 
     57 /*
     58  * Emulate qsort_r using thread local storage to access the thunk data.
     59  */
     60 
     61 #include <cutils/threads.h>
     62 
     63 static thread_store_t compar_data_key = THREAD_STORE_INITIALIZER;
     64 
     65 struct compar_data {
     66     void* thunk;
     67     int (*compar)(void*, const void* , const void*);
     68 };
     69 
     70 static int compar_wrapper(const void* a, const void* b) {
     71     struct compar_data* compar_data = (struct compar_data*)thread_store_get(&compar_data_key);
     72     return compar_data->compar(compar_data->thunk, a, b);
     73 }
     74 
     75 void qsort_r_compat(void* base, size_t nel, size_t width, void* thunk,
     76         int (*compar)(void*, const void* , const void*)) {
     77     struct compar_data compar_data;
     78     compar_data.thunk = thunk;
     79     compar_data.compar = compar;
     80     thread_store_set(&compar_data_key, &compar_data, NULL);
     81     qsort(base, nel, width, compar_wrapper);
     82 }
     83 
     84 #endif
     85