aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxutil/allocate.c
blob: 600ebabda601d76db3b478ebce8e3164711b4270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "allocate.h"

/** @file
 * Support for allocating memory.
 * Usable from user code or kernel code (with __KERNEL__ defined).
 * In user code will use GC if USE_GC is defined.
 */

#ifdef __KERNEL__
/*----------------------------------------------------------------------------*/
#  include <linux/config.h>
#  include <linux/slab.h>
#  include <linux/string.h>
#  include <linux/types.h>

#  define DEFAULT_TYPE    0
#  define MALLOC(n, type) kmalloc(n, type)
#  define FREE(ptr)       kfree(ptr)

/*----------------------------------------------------------------------------*/
#else /* ! __KERNEL__ */

#  include <stdlib.h>
#  include <string.h>

#  define DEFAULT_TYPE    0

#ifdef USE_GC
#  include "gc.h"
#  define MALLOC(n, typ)  GC_malloc(n)
#  define FREE(ptr)       (ptr=NULL)
//typedef void *GC_PTR;
//GC_PTR (*GC_oom_fn)(size_t n);
#else
#  define MALLOC(n, type) malloc(n)
#  define FREE(ptr)       free(ptr)
#endif

/*----------------------------------------------------------------------------*/
#endif

/** Function to call when memory cannot be allocated. */
AllocateFailedFn *allocate_failed_fn = NULL;

/** Allocate memory and zero it.
 * The type is only relevant when calling from kernel code,
 * from user code it is ignored.
 * In kernel code the values accepted by kmalloc can be used:
 * GFP_USER, GFP_ATOMIC, GFP_KERNEL.
 *
 * @param size number of bytes to allocate
 * @param type memory type to allocate (kernel only)
 * @return pointer to the allocated memory or zero
 * if malloc failed
 */
void *allocate_type(int size, int type){
    void *p = MALLOC(size, type);
    if(p){
        memzero(p, size);
    } else if(allocate_failed_fn){
        allocate_failed_fn(size, type);
    }
    return p;
}

/** Allocate memory and zero it.
 *
 * @param size number of bytes to allocate
 * @return pointer to the allocated memory or zero
 * if malloc failed
 */
void *allocate(int size){
    return allocate_type(size, DEFAULT_TYPE);
}

/** Free memory allocated by allocate().
 * No-op if 'p' is null.
 *
 * @param p memory to free
 */
void deallocate(void *p){
    if(p){
        FREE(p);
    }
}

/** Set bytes to zero.
 * No-op if 'p' is null.
 *
 * @param p memory to zero
 * @param size number of bytes to zero
 */
void memzero(void *p, int size){
    if(p){
        memset(p, 0, (size_t)size);
    }
}