source: trunk/packages/xen-3.1/xen-3.1/tools/vnet/libxutil/allocate.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 3.1 KB
Line 
1/*
2 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#include "allocate.h"
20
21/** @file
22 * Support for allocating memory.
23 * Usable from user code or kernel code (with __KERNEL__ defined).
24 * In user code will use GC if USE_GC is defined.
25 */
26
27#ifdef __KERNEL__
28/*----------------------------------------------------------------------------*/
29#  include <linux/config.h>
30#  include <linux/slab.h>
31#  include <linux/string.h>
32#  include <linux/types.h>
33
34#  define DEFAULT_TYPE    0
35#  define MALLOC(n, type) kmalloc(n, type)
36#  define FREE(ptr)       kfree(ptr)
37
38/*----------------------------------------------------------------------------*/
39#else /* ! __KERNEL__ */
40
41#  include <stdlib.h>
42#  include <string.h>
43
44#  define DEFAULT_TYPE    0
45
46#ifdef USE_GC
47#  include "gc.h"
48#  define MALLOC(n, typ)  GC_malloc(n)
49#  define FREE(ptr)       (ptr=NULL)
50//typedef void *GC_PTR;
51//GC_PTR (*GC_oom_fn)(size_t n);
52#else
53#  define MALLOC(n, type) malloc(n)
54#  define FREE(ptr)       free(ptr)
55#endif
56
57/*----------------------------------------------------------------------------*/
58#endif
59
60/** Function to call when memory cannot be allocated. */
61AllocateFailedFn *allocate_failed_fn = NULL;
62
63/** Allocate memory and zero it.
64 * The type is only relevant when calling from kernel code,
65 * from user code it is ignored.
66 * In kernel code the values accepted by kmalloc can be used:
67 * GFP_USER, GFP_ATOMIC, GFP_KERNEL.
68 *
69 * @param size number of bytes to allocate
70 * @param type memory type to allocate (kernel only)
71 * @return pointer to the allocated memory or zero
72 * if malloc failed
73 */
74void *allocate_type(int size, int type){
75    void *p = MALLOC(size, type);
76    if(p){
77        memzero(p, size);
78    } else if(allocate_failed_fn){
79        allocate_failed_fn(size, type);
80    }
81    return p;
82}
83
84/** Allocate memory and zero it.
85 *
86 * @param size number of bytes to allocate
87 * @return pointer to the allocated memory or zero
88 * if malloc failed
89 */
90void *allocate(int size){
91    return allocate_type(size, DEFAULT_TYPE);
92}
93
94/** Free memory allocated by allocate().
95 * No-op if 'p' is null.
96 *
97 * @param p memory to free
98 */
99void deallocate(void *p){
100    if(p){
101        FREE(p);
102    }
103}
104
105/** Set bytes to zero.
106 * No-op if 'p' is null.
107 *
108 * @param p memory to zero
109 * @param size number of bytes to zero
110 */
111void memzero(void *p, int size){
112    if(p){
113        memset(p, 0, (size_t)size);
114    }
115}
116
Note: See TracBrowser for help on using the repository browser.