~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/mulalloc.cc

Merged Padraig.

Show diffs side-by-side

added added

removed removed

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