~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/memory/multi_malloc.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

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_priv.h"
 
16
#include "config.h"
 
17
 
17
18
#include <stdarg.h>
18
 
 
 
19
#include <string.h>
 
20
#include <stdlib.h>
 
21
 
 
22
#include "drizzled/memory/multi_malloc.h"
 
23
#include "drizzled/definitions.h"
 
24
 
 
25
namespace drizzled
 
26
{
 
27
namespace memory
 
28
{
19
29
/*
20
30
  Malloc many pointers at the same time
21
31
  Only ptr1 can be free'd, and doing this will free all
23
33
  memory area.
24
34
 
25
35
  SYNOPSIS
26
 
    my_multi_malloc()
27
 
      myFlags              Flags
 
36
    multi_malloc()
 
37
      zerofill             Whether or not to fill with 0
28
38
        ptr1, length1      Multiple arguments terminated by null ptr
29
39
        ptr2, length2      ...
30
40
        ...
31
41
        NULL
32
42
*/
33
43
 
34
 
void* my_multi_malloc(myf myFlags, ...)
 
44
void* multi_malloc(bool zerofill, ...)
35
45
{
36
46
  va_list args;
37
 
  char **ptr,*start,*res;
 
47
  void **ptr, *start;
 
48
  char *res;
38
49
  size_t tot_length,length;
39
50
 
40
 
  va_start(args,myFlags);
 
51
  va_start(args, zerofill);
41
52
  tot_length=0;
42
 
  while ((ptr=va_arg(args, char **)))
 
53
  while ((ptr=va_arg(args, void **)))
43
54
  {
44
 
    length=va_arg(args,uint);
 
55
    /*
 
56
     * This must be unsigned int, not size_t.
 
57
     * Otherwise, everything breaks.
 
58
     */
 
59
    length=va_arg(args, unsigned int);
45
60
    tot_length+=ALIGN_SIZE(length);
46
61
  }
47
62
  va_end(args);
48
63
 
49
 
  if (!(start=(char *) my_malloc(tot_length,myFlags)))
50
 
    return(0); /* purecov: inspected */
 
64
  if (!(start= malloc(tot_length)))
 
65
    return(0);
 
66
  if (zerofill)
 
67
    memset(start, 0, tot_length);
51
68
 
52
 
  va_start(args,myFlags);
53
 
  res=start;
54
 
  while ((ptr=va_arg(args, char **)))
 
69
  va_start(args, zerofill);
 
70
  res= static_cast<char *>(start);
 
71
  while ((ptr=va_arg(args, void **)))
55
72
  {
56
73
    *ptr=res;
57
 
    length=va_arg(args,uint);
58
 
    res+=ALIGN_SIZE(length);
 
74
    length=va_arg(args,unsigned int);
 
75
    res+= ALIGN_SIZE(length);
59
76
  }
60
77
  va_end(args);
61
 
  return((void*) start);
 
78
  return start;
62
79
}
 
80
 
 
81
} /* namespace memory */
 
82
} /* namespace drizzled */