~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/memory/multi_malloc.cc

  • Committer: Eric Day
  • Date: 2009-10-31 21:53:33 UTC
  • mfrom: (1200 staging)
  • mto: This revision was merged to the branch mainline in revision 1202.
  • Revision ID: eday@oddments.org-20091031215333-j94bjoanwmi68p6f
Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys/mysys_priv.h"
 
16
#include "drizzled/global.h"
 
17
 
17
18
#include <stdarg.h>
18
19
#include <string.h>
19
20
#include <stdlib.h>
20
21
 
 
22
#include "drizzled/memory/multi_malloc.h"
 
23
 
 
24
namespace drizzled
 
25
{
 
26
namespace memory
 
27
{
21
28
/*
22
29
  Malloc many pointers at the same time
23
30
  Only ptr1 can be free'd, and doing this will free all
25
32
  memory area.
26
33
 
27
34
  SYNOPSIS
28
 
    my_multi_malloc()
29
 
      myFlags              Flags
 
35
    multi_malloc()
 
36
      zerofill             Whether or not to fill with 0
30
37
        ptr1, length1      Multiple arguments terminated by null ptr
31
38
        ptr2, length2      ...
32
39
        ...
33
40
        NULL
34
41
*/
35
42
 
36
 
void* my_multi_malloc(myf myFlags, ...)
 
43
void* multi_malloc(bool zerofill, ...)
37
44
{
38
45
  va_list args;
39
 
  char **ptr,*start,*res;
 
46
  void **ptr, *start;
 
47
  char *res;
40
48
  size_t tot_length,length;
41
49
 
42
 
  va_start(args,myFlags);
 
50
  va_start(args, zerofill);
43
51
  tot_length=0;
44
 
  while ((ptr=va_arg(args, char **)))
 
52
  while ((ptr=va_arg(args, void **)))
45
53
  {
46
 
    length=va_arg(args,uint);
 
54
    /*
 
55
     * This must be unsigned int, not size_t.
 
56
     * Otherwise, everything breaks.
 
57
     */
 
58
    length=va_arg(args, unsigned int);
47
59
    tot_length+=ALIGN_SIZE(length);
48
60
  }
49
61
  va_end(args);
50
62
 
51
 
  if (!(start=(char *) malloc(tot_length)))
 
63
  if (!(start= malloc(tot_length)))
52
64
    return(0);
53
 
  if (myFlags & MY_ZEROFILL)
 
65
  if (zerofill)
54
66
    memset(start, 0, tot_length);
55
67
 
56
 
  va_start(args,myFlags);
57
 
  res=start;
58
 
  while ((ptr=va_arg(args, char **)))
 
68
  va_start(args, zerofill);
 
69
  res=reinterpret_cast<char *>(start);
 
70
  while ((ptr=va_arg(args, void **)))
59
71
  {
60
72
    *ptr=res;
61
73
    length=va_arg(args,uint);
62
 
    res+=ALIGN_SIZE(length);
 
74
    res+= ALIGN_SIZE(length);
63
75
  }
64
76
  va_end(args);
65
 
  return((void*) start);
 
77
  return start;
66
78
}
 
79
 
 
80
} /* namespace memory */
 
81
} /* namespace drizzled */