annotate lib/lua/lua-5.2.2/lmem.c @ 1996:f7e8c4d4a01b

sound id fix
author zipi
date Sun, 03 Nov 2013 14:29:45 +0000
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lmem.c,v 1.84 2012/05/23 15:41:53 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Interface to Memory Manager
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stddef.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define lmem_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "lgc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 ** About the realloc function:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 ** (`osize' is the old size, `nsize' is the new size)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28 **
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 ** matter 'x').
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 **
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 ** * frealloc(ud, p, x, 0) frees the block `p'
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 ** (in this specific case, frealloc must return NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 ** particularly, frealloc(ud, NULL, 0, 0) does nothing
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 ** (which is equivalent to free(NULL) in ANSI C)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 **
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 ** frealloc returns NULL if it cannot create or reallocate the area
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 ** (any reallocation to an equal or smaller size cannot fail!)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 #define MINSIZEARRAY 4
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 int limit, const char *what) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 void *newblock;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 int newsize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 if (*size >= limit/2) { /* cannot double it? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 if (*size >= limit) /* cannot grow even a little? */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 luaG_runerror(L, "too many %s (limit is %d)", what, limit);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 newsize = limit; /* still have at least one free place */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 else {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 newsize = (*size)*2;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 if (newsize < MINSIZEARRAY)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 newsize = MINSIZEARRAY; /* minimum size */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 *size = newsize; /* update only when everything else is OK */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 return newblock;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 l_noret luaM_toobig (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 luaG_runerror(L, "memory allocation error: block too big");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 ** generic allocation routine.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75 void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 void *newblock;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 size_t realosize = (block) ? osize : 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79 lua_assert((realosize == 0) == (block == NULL));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 #if defined(HARDMEMTESTS)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 if (nsize > realosize && g->gcrunning)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 luaC_fullgc(L, 1); /* force a GC whenever possible */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 if (newblock == NULL && nsize > 0) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 api_check(L, nsize > realosize,
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 "realloc cannot fail when shrinking a block");
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 if (g->gcrunning) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 luaC_fullgc(L, 1); /* try to free some memory... */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 if (newblock == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 luaD_throw(L, LUA_ERRMEM);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 lua_assert((nsize == 0) == (newblock == NULL));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 g->GCdebt = (g->GCdebt + nsize) - realosize;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 return newblock;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99