5d24b140e24409f173abf335e96ccb35f153c810
2 * Memory allocation routines.
3 * Copyright (c) 1998 New Generation Software (NGS) Oy
5 * Author: Markku Rossi <mtr@ngs.fi>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/src/alloc.c,v $
36 #if JS_DEBUG_MEMORY_LEAKS
40 struct mem_debug_st
*next
;
41 struct mem_debug_st
*prev
;
47 typedef struct mem_debug_st MemDebug
;
49 static MemDebug
*mem_debug_blocks
= NULL
;
51 int mem_debug_balance
= 0;
53 unsigned int alloc_fail
= 0;
54 unsigned int alloc_count
= 0;
57 register_block (MemDebug
*b
, size_t size
, char *file
, int line
)
67 b
->next
= mem_debug_blocks
;
68 mem_debug_blocks
->prev
= b
;
77 unregister_block (MemDebug
*b
)
81 fprintf (stderr
, "freeing the same block twise\n");
86 b
->next
->prev
= b
->prev
;
89 b
->prev
->next
= b
->next
;
91 mem_debug_blocks
= b
->next
;
102 return alloc_fail
!= 0 && ++alloc_count
>= alloc_fail
;
107 js_alloc_dump_blocks ()
110 unsigned int bytes
= 0;
112 fprintf (stderr
, "js_alloc_dump_blocks(): #blocks=%d\n", mem_debug_balance
);
114 for (b
= mem_debug_blocks
; b
; b
= b
->next
)
116 fprintf (stderr
, "%s:%d: %lu\n", b
->file
, b
->line
, b
->size
);
120 fprintf (stderr
, "leaks=%u\n", bytes
);
126 js_malloc_i (JSVirtualMachine
*vm
, size_t size
, char *file
, int line
)
130 ptr
= malloc (sizeof (MemDebug
) + size
);
131 if (check_fail () || ptr
== NULL
)
135 sprintf (vm
->error
, "VM: memory exhausted");
142 register_block (ptr
, size
, file
, line
);
144 return (unsigned char *) ptr
+ sizeof (MemDebug
);
149 js_calloc_i (JSVirtualMachine
*vm
, size_t num
, size_t size
, char *file
,
154 ptr
= malloc (sizeof (MemDebug
) + num
* size
);
155 if (check_fail () || ptr
== NULL
)
159 sprintf (vm
->error
, "VM: memory exhausted");
166 memset (ptr
, 0, sizeof (MemDebug
) + num
* size
);
167 register_block (ptr
, num
* size
, file
, line
);
169 return (unsigned char *) ptr
+ sizeof (MemDebug
);
174 js_realloc_i (JSVirtualMachine
*vm
, void *ptr
, size_t size
, char *file
,
181 return js_malloc_i (vm
, size
, file
, line
);
183 nptr
= js_malloc_i (vm
, size
, file
, line
);
188 sprintf (vm
->error
, "VM: memory exhausted");
195 b
= (MemDebug
*) ((unsigned char *) ptr
- sizeof (MemDebug
));
197 memcpy (nptr
, ptr
, size
< b
->size
? size
: b
->size
);
213 b
= (MemDebug
*) ((unsigned char *) ptr
- sizeof (MemDebug
));
214 unregister_block (b
);
221 js_strdup_i (JSVirtualMachine
*vm
, const char *str
, char *file
, int line
)
225 tmp
= js_malloc_i (vm
, strlen (str
) + 1, file
, line
);
234 #else /* not JS_DEBUG_MEMORY_LEAKS */
237 js_malloc (JSVirtualMachine
*vm
, size_t size
)
242 if (ptr
== NULL
&& vm
!= NULL
)
244 sprintf (vm
->error
, "VM: memory exhausted");
253 js_calloc (JSVirtualMachine
*vm
, size_t num
, size_t size
)
257 ptr
= calloc (num
, size
);
258 if (ptr
== NULL
&& vm
!= NULL
)
260 sprintf (vm
->error
, "VM: memory exhausted");
269 js_realloc (JSVirtualMachine
*vm
, void *ptr
, size_t size
)
274 return js_malloc (vm
, size
);
276 nptr
= realloc (ptr
, size
);
277 if (nptr
== NULL
&& vm
!= NULL
)
279 sprintf (vm
->error
, "VM: memory exhausted");
298 js_strdup (JSVirtualMachine
*vm
, const char *str
)
302 tmp
= js_malloc (vm
, strlen (str
) + 1);
311 #endif /* not JS_DEBUG_MEMORY_LEAKS */