r502: modified ldb to allow the use of an external pool memory
[kai/samba.git] / source / lib / ldb / common / util.c
index d198a1ad921c24981f640d33550d7836be7a7c58..68537a78640aa9cc3aa0d960bcd1f7fd16ce7854 100644 (file)
 #include "includes.h"
 
 
-#define MAX_MALLOC_SIZE 0x7fffffff
-
-/*
-  realloc an array, checking for integer overflow in the array size
-*/
-void *realloc_array(void *ptr, size_t el_size, unsigned count)
-{
-       if (count == 0 ||
-           count >= MAX_MALLOC_SIZE/el_size) {
-               return NULL;
-       }
-       if (!ptr) {
-               return malloc(el_size * count);
-       }
-       return realloc(ptr, el_size * count);
-}
-
-
 /*
   find an element in a list, using the given comparison function and
   assuming that the list is already sorted using comp_fn
@@ -73,28 +55,30 @@ int list_find(const void *needle,
        max_i = nmemb-1;
 
        while (min_i < max_i) {
-               size_t test_t;
                int r;
 
                test_i = (min_i + max_i) / 2;
-               r = comp_fn(needle, *(void **)(base_p + (size * test_i)));
+               r = comp_fn(needle, *(void * const *)(base_p + (size * test_i)));
                if (r == 0) {
                        /* scan back for first element */
-                       while (test_t > 0 &&
-                              comp_fn(needle, *(void **)(base_p + (size * (test_i-1)))) == 0) {
+                       while (test_i > 0 &&
+                              comp_fn(needle, *(void * const *)(base_p + (size * (test_i-1)))) == 0) {
                                test_i--;
                        }
                        return test_i;
                }
-               if (r == -1) {
+               if (r < 0) {
+                       if (test_i == 0) {
+                               return -1;
+                       }
                        max_i = test_i - 1;
                }
-               if (r == 1) {
+               if (r > 0) {
                        min_i = test_i + 1;
                }
        }
 
-       if (comp_fn(needle, *(void **)(base_p + (size * min_i))) == 0) {
+       if (comp_fn(needle, *(void * const *)(base_p + (size * min_i))) == 0) {
                return min_i;
        }