ia64/xen-unstable

view extras/mini-os/include/xmalloc.h @ 16838:945820bfedb6

minios: POSIX fixes
Fixes some functions which are POSIX. Also make them ifndef HAVE_LIBC.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 22 14:20:22 2008 +0000 (2008-01-22)
parents 5f3c236d1711
children 433d1b26fd51
line source
1 #ifndef __XMALLOC_H__
2 #define __XMALLOC_H__
4 #ifdef HAVE_LIBC
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)
12 #else
14 #define DEFAULT_ALIGN (sizeof(unsigned long))
15 #define malloc(size) _xmalloc(size, DEFAULT_ALIGN)
16 #define free(ptr) xfree(ptr)
17 #define realloc(ptr, size) _realloc(ptr, size)
19 /* Free any of the above. */
20 extern void xfree(const void *);
22 /* Underlying functions */
23 extern void *_xmalloc(size_t size, size_t align);
24 extern void *_realloc(void *ptr, size_t size);
26 #endif
28 static inline void *_xmalloc_array(size_t size, size_t align, size_t num)
29 {
30 /* Check for overflow. */
31 if (size && num > UINT_MAX / size)
32 return NULL;
33 return _xmalloc(size * num, align);
34 }
36 /* Allocate space for typed object. */
37 #define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))
39 /* Allocate space for array of typed objects. */
40 #define xmalloc_array(_type, _num) ((_type *)_xmalloc_array(sizeof(_type), __alignof__(_type), _num))
42 #endif /* __XMALLOC_H__ */