drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
multi_malloc.cc
1/* Copyright (C) 2000 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include <config.h>
17
18#include <stdarg.h>
19#include <string.h>
20#include <stdlib.h>
21
22#include <drizzled/memory/multi_malloc.h>
24
25namespace drizzled {
26namespace memory {
27
28/*
29 Malloc many pointers at the same time
30 Only ptr1 can be free'd, and doing this will free all
31 the memory allocated. ptr2, etc all point inside big allocated
32 memory area.
33
34 SYNOPSIS
35 multi_malloc()
36 zerofill Whether or not to fill with 0
37 ptr1, length1 Multiple arguments terminated by null ptr
38 ptr2, length2 ...
39 ...
40 NULL
41*/
42
43void* multi_malloc(bool zerofill, ...)
44{
45 va_list args;
46 void **ptr, *start;
47 char *res;
48 size_t tot_length,length;
49
50 va_start(args, zerofill);
51 tot_length=0;
52 while ((ptr=va_arg(args, void **)))
53 {
54 /*
55 * This must be unsigned int, not size_t.
56 * Otherwise, everything breaks.
57 */
58 length=va_arg(args, unsigned int);
59 tot_length+=ALIGN_SIZE(length);
60 }
61 va_end(args);
62
63#ifdef HAVE_VALGRIND
64 if (!(start= calloc(0, tot_length)))
65 return 0;
66#else
67 start= malloc(tot_length);
68 if (zerofill)
69 memset(start, 0, tot_length);
70#endif
71
72 va_start(args, zerofill);
73 res= static_cast<char *>(start);
74 while ((ptr=va_arg(args, void **)))
75 {
76 *ptr=res;
77 length=va_arg(args,unsigned int);
78 res+= ALIGN_SIZE(length);
79 }
80 va_end(args);
81 return start;
82}
83
84} /* namespace memory */
85} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.