~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to extras/mini-os/include/xmalloc.h

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __XMALLOC_H__
 
2
#define __XMALLOC_H__
 
3
 
 
4
#ifdef HAVE_LIBC
 
5
 
 
6
#include <stdlib.h>
 
7
#include <malloc.h>
 
8
/* Allocate space for typed object. */
 
9
#define _xmalloc(size, align) memalign(align, size)
 
10
#define xfree(ptr) free(ptr)
 
11
 
 
12
#else
 
13
 
 
14
#include <limits.h>
 
15
 
 
16
#define DEFAULT_ALIGN (sizeof(unsigned long))
 
17
#define malloc(size) _xmalloc(size, DEFAULT_ALIGN)
 
18
#define free(ptr) xfree(ptr)
 
19
#define realloc(ptr, size) _realloc(ptr, size)
 
20
 
 
21
/* Free any of the above. */
 
22
extern void xfree(const void *);
 
23
 
 
24
/* Underlying functions */
 
25
extern void *_xmalloc(size_t size, size_t align);
 
26
extern void *_realloc(void *ptr, size_t size);
 
27
 
 
28
#endif
 
29
 
 
30
static inline void *_xmalloc_array(size_t size, size_t align, size_t num)
 
31
{
 
32
        /* Check for overflow. */
 
33
        if (size && num > UINT_MAX / size)
 
34
                return NULL;
 
35
        return _xmalloc(size * num, align);
 
36
}
 
37
 
 
38
/* Allocate space for typed object. */
 
39
#define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
 
40
 
 
41
/* Allocate space for array of typed objects. */
 
42
#define xmalloc_array(_type, _num) ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
 
43
 
 
44
#endif /* __XMALLOC_H__ */