2
* Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
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.
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.
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
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.
28
/*----------------------------------------------------------------------------*/
29
# include <linux/config.h>
30
# include <linux/slab.h>
31
# include <linux/string.h>
32
# include <linux/types.h>
34
# define DEFAULT_TYPE 0
35
# define MALLOC(n, type) kmalloc(n, type)
36
# define FREE(ptr) kfree(ptr)
38
/*----------------------------------------------------------------------------*/
39
#else /* ! __KERNEL__ */
44
# define DEFAULT_TYPE 0
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);
53
# define MALLOC(n, type) malloc(n)
54
# define FREE(ptr) free(ptr)
57
/*----------------------------------------------------------------------------*/
60
/** Function to call when memory cannot be allocated. */
61
AllocateFailedFn *allocate_failed_fn = NULL;
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.
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
74
void *allocate_type(int size, int type){
75
void *p = MALLOC(size, type);
78
} else if(allocate_failed_fn){
79
allocate_failed_fn(size, type);
84
/** Allocate memory and zero it.
86
* @param size number of bytes to allocate
87
* @return pointer to the allocated memory or zero
90
void *allocate(int size){
91
return allocate_type(size, DEFAULT_TYPE);
94
/** Free memory allocated by allocate().
95
* No-op if 'p' is null.
97
* @param p memory to free
99
void deallocate(void *p){
105
/** Set bytes to zero.
106
* No-op if 'p' is null.
108
* @param p memory to zero
109
* @param size number of bytes to zero
111
void memzero(void *p, int size){
113
memset(p, 0, (size_t)size);