4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb utility functions
30 * Description: miscellanous utility functions for ldb
32 * Author: Andrew Tridgell
38 #define MAX_MALLOC_SIZE 0x7fffffff
41 realloc an array, checking for integer overflow in the array size
43 void *realloc_array(void *ptr, size_t el_size, unsigned count)
46 count >= MAX_MALLOC_SIZE/el_size) {
50 return malloc(el_size * count);
52 return realloc(ptr, el_size * count);
57 find an element in a list, using the given comparison function and
58 assuming that the list is already sorted using comp_fn
60 return -1 if not found, or the index of the first occurance of needle if found
62 int list_find(const void *needle,
63 const void *base, size_t nmemb, size_t size, comparison_fn_t comp_fn)
65 const char *base_p = base;
66 size_t min_i, max_i, test_i;
75 while (min_i < max_i) {
79 test_i = (min_i + max_i) / 2;
80 r = comp_fn(needle, *(void * const *)(base_p + (size * test_i)));
82 /* scan back for first element */
84 comp_fn(needle, *(void * const *)(base_p + (size * (test_i-1)))) == 0) {
97 if (comp_fn(needle, *(void * const *)(base_p + (size * min_i))) == 0) {