MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mulalloc.c
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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
15 
16 #include "mysys_priv.h"
17 #include <stdarg.h>
18 
19 /*
20  Malloc many pointers at the same time
21  Only ptr1 can be free'd, and doing this will free all
22  the memory allocated. ptr2, etc all point inside big allocated
23  memory area.
24 
25  SYNOPSIS
26  my_multi_malloc()
27  myFlags Flags
28  ptr1, length1 Multiple arguments terminated by null ptr
29  ptr2, length2 ...
30  ...
31  NULL
32 */
33 
34 void* my_multi_malloc(myf myFlags, ...)
35 {
36  va_list args;
37  char **ptr,*start,*res;
38  size_t tot_length,length;
39  DBUG_ENTER("my_multi_malloc");
40 
41  va_start(args,myFlags);
42  tot_length=0;
43  while ((ptr=va_arg(args, char **)))
44  {
45  length=va_arg(args,uint);
46  tot_length+=ALIGN_SIZE(length);
47  }
48  va_end(args);
49 
50  if (!(start=(char *) my_malloc(tot_length,myFlags)))
51  DBUG_RETURN(0); /* purecov: inspected */
52 
53  va_start(args,myFlags);
54  res=start;
55  while ((ptr=va_arg(args, char **)))
56  {
57  *ptr=res;
58  length=va_arg(args,uint);
59  res+=ALIGN_SIZE(length);
60  }
61  va_end(args);
62  DBUG_RETURN((void*) start);
63 }