1 | /* Copyright (c) 2000, 2002, 2003, 2007 MySQL AB |
2 | Use is subject to license terms |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; version 2 of the License. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
16 | |
17 | #include "mysys_priv.h" |
18 | #include <stdarg.h> |
19 | |
20 | /* |
21 | Malloc many pointers at the same time |
22 | Only ptr1 can be free'd, and doing this will free all |
23 | the memory allocated. ptr2, etc all point inside big allocated |
24 | memory area. |
25 | |
26 | SYNOPSIS |
27 | my_multi_malloc() |
28 | myFlags Flags |
29 | ptr1, length1 Multiple arguments terminated by null ptr |
30 | ptr2, length2 ... |
31 | ... |
32 | NULL |
33 | */ |
34 | |
35 | void* my_multi_malloc(myf myFlags, ...) |
36 | { |
37 | va_list args; |
38 | char **ptr,*start,*res; |
39 | size_t tot_length,length; |
40 | DBUG_ENTER("my_multi_malloc" ); |
41 | |
42 | va_start(args,myFlags); |
43 | tot_length=0; |
44 | while ((ptr=va_arg(args, char **))) |
45 | { |
46 | length=va_arg(args,uint); |
47 | tot_length+=ALIGN_SIZE(length); |
48 | } |
49 | va_end(args); |
50 | |
51 | if (!(start=(char *) my_malloc(tot_length,myFlags))) |
52 | DBUG_RETURN(0); /* purecov: inspected */ |
53 | |
54 | va_start(args,myFlags); |
55 | res=start; |
56 | while ((ptr=va_arg(args, char **))) |
57 | { |
58 | *ptr=res; |
59 | length=va_arg(args,uint); |
60 | res+=ALIGN_SIZE(length); |
61 | } |
62 | va_end(args); |
63 | DBUG_RETURN((void*) start); |
64 | } |
65 | |
66 | |
67 | /* |
68 | Same as my_multi_malloc, but each entry can be over 4G |
69 | |
70 | SYNOPSIS |
71 | my_multi_malloc() |
72 | myFlags Flags |
73 | ptr1, length1 Multiple arguments terminated by null ptr |
74 | ptr2, length2 ... |
75 | ... |
76 | NULL |
77 | */ |
78 | |
79 | void *my_multi_malloc_large(myf myFlags, ...) |
80 | { |
81 | va_list args; |
82 | char **ptr,*start,*res; |
83 | ulonglong tot_length,length; |
84 | DBUG_ENTER("my_multi_malloc" ); |
85 | |
86 | va_start(args,myFlags); |
87 | tot_length=0; |
88 | while ((ptr=va_arg(args, char **))) |
89 | { |
90 | length=va_arg(args,ulonglong); |
91 | tot_length+=ALIGN_SIZE(length); |
92 | } |
93 | va_end(args); |
94 | |
95 | if (!(start=(char *) my_malloc((size_t) tot_length, myFlags))) |
96 | DBUG_RETURN(0); /* purecov: inspected */ |
97 | |
98 | va_start(args,myFlags); |
99 | res=start; |
100 | while ((ptr=va_arg(args, char **))) |
101 | { |
102 | *ptr=res; |
103 | length=va_arg(args,ulonglong); |
104 | res+=ALIGN_SIZE(length); |
105 | } |
106 | va_end(args); |
107 | DBUG_RETURN((void*) start); |
108 | } |
109 | |