[go: up one dir, main page]

File: alloc.c

package info (click to toggle)
ucl 1.03-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,884 kB
  • ctags: 9,973
  • sloc: sh: 11,725; asm: 10,461; ansic: 5,057; makefile: 150
file content (125 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download | duplicates (8)
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
117
118
119
120
121
122
123
124
125
/* alloc.c -- memory allocation

   This file is part of the UCL data compression library.

   Copyright (C) 1996-2004 Markus Franz Xaver Johannes Oberhumer
   All Rights Reserved.

   The UCL library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   The UCL 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the UCL library; see the file COPYING.
   If not, write to the Free Software Foundation, Inc.,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   Markus F.X.J. Oberhumer
   <markus@oberhumer.com>
   http://www.oberhumer.com/opensource/ucl/
 */


#include "ucl_conf.h"


/***********************************************************************
// implementation
************************************************************************/

#if defined(__UCL_MMODEL_HUGE)

#define acc_hsize_t             ucl_uint
#define acc_hvoid_p             ucl_voidp
#define ACCLIB_PUBLIC(r,f)      static r __UCL_CDECL f
#define acc_halloc              ucl_malloc_internal
#define acc_hfree               ucl_free_internal
#include "acc/acclib/halloc.ch"
#undef ACCLIB_PUBLIC

#else

UCL_PRIVATE(ucl_voidp)
ucl_malloc_internal(ucl_uint size)
{
    ucl_voidp p = NULL;
    if (size < ~(size_t)0)
        p = (ucl_voidp) malloc((size_t) size);
    return p;
}


UCL_PRIVATE(void)
ucl_free_internal(ucl_voidp p)
{
    if (p)
        free(p);
}

#endif


/***********************************************************************
// public interface using the global hooks
************************************************************************/

/* global allocator hooks */
static ucl_malloc_hook_t ucl_malloc_hook = ucl_malloc_internal;
static ucl_free_hook_t ucl_free_hook = ucl_free_internal;

UCL_PUBLIC(void)
ucl_set_malloc_hooks(ucl_malloc_hook_t a, ucl_free_hook_t f)
{
    ucl_malloc_hook = ucl_malloc_internal;
    ucl_free_hook = ucl_free_internal;
    if (a)
        ucl_malloc_hook = a;
    if (f)
        ucl_free_hook = f;
}

UCL_PUBLIC(void)
ucl_get_malloc_hooks(ucl_malloc_hook_t* a, ucl_free_hook_t* f)
{
    if (a)
        *a = ucl_malloc_hook;
    if (f)
        *f = ucl_free_hook;
}


UCL_PUBLIC(ucl_voidp)
ucl_malloc(ucl_uint size)
{
    if (size <= 0)
        return NULL;
    return ucl_malloc_hook(size);
}

UCL_PUBLIC(ucl_voidp)
ucl_alloc(ucl_uint nelems, ucl_uint size)
{
    ucl_uint s = nelems * size;
    if (nelems <= 0 || s / nelems != size)
        return NULL;
    return ucl_malloc(s);
}


UCL_PUBLIC(void)
ucl_free(ucl_voidp p)
{
    if (p)
        ucl_free_hook(p);
}


/*
vi:ts=4:et
*/