1 /*******************************************************************
5 * Memory management component (body).
7 * Copyright 1996, 1997 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * Portions Copyright 1998 by Michal Necasek
12 * This file is part of the FreeType project, and may only be used
13 * modified and distributed under the terms of the FreeType project
14 * license, LICENSE.TXT. By continuing to use, modify, or distribute
15 * this file you indicate that you have read the license and
16 * understand and accept it fully.
19 * Changes between 1.1 and 1.2:
21 * - the font pool is gone.
23 * - introduced the FREE macro and the Free function for
24 * future use in destructors.
26 * - Init_FontPool() is now a macro to allow the compilation of
27 * 'legacy' applications (all four test programs have been updated).
29 * Note: This code was slightly adapted for use in the OS/2
30 * Font Driver (FreeType/2).
32 ******************************************************************/
46 /* -------------------- debugging defs ----------------------- */
47 /* DEBUG_MEM creates a file and logs all actions to it */
50 static HFILE MemLogHandle
= NULLHANDLE
;
51 static ULONG Written
= 0;
52 static char log
[2048] = "";
53 static char buf
[2048] = "";
56 char* itoa10( int i
, char* buffer
) {
74 *ptr
= (char) (i
% 10 + '0');
92 static const char* hexstr
= "0123456789abcdef";
94 char* itohex2( int i
, char* buffer
)
96 buffer
[0] = hexstr
[ (i
>> 12) & 0xF ];
97 buffer
[1] = hexstr
[ (i
>> 8 ) & 0xF ];
98 buffer
[2] = hexstr
[ (i
>> 4 ) & 0xF ];
99 buffer
[3] = hexstr
[ (i
) & 0xF ];
104 char* itohex4( long i
, char* buffer
)
106 itohex2( (i
>> 16) & 0xFFFF, buffer
);
107 /* We separate the high and low part with a dot to make it */
110 itohex2( i
& 0xFFFF, buffer
+5 );
114 #define COPY(s) strcpy(log, s)
115 #define CAT(s) strcat(log, s)
116 #define CATI(v) strcat(log, itoa10( (int)v, buf ))
117 #define CATH(v) strcat(log, itohex4( (long)v, buf ))
118 #define CATW(v) strcat(log, itohex2( (short)v, buf ))
119 #define WRITE DosWrite(MemLogHandle, log, strlen(log), &Written)
120 #define ERRRET(e) { COPY("Error at "); \
135 #define ERRRET(e) return(e);
137 #endif /* DEBUG_MEM */
141 /* TRACK_MEM allows online tracking of memory usage online (via shared */
142 /* memory). It is used in conjunction with the FTMEM utility. */
145 /* name of shared memory used for memory usage reporting */
146 #define MEM_NAME "\\sharemem\\freetype"
148 /* structure containing memory usage information */
149 typedef struct _INFOSTRUCT
{
150 ULONG signature
; /* signature (0x46524545, 'FREE') */
151 ULONG used
; /* bytes actually used */
152 ULONG maxused
; /* maximum amount ever used */
153 ULONG num_err
; /* number of (de)allocation errors */
154 } INFOSTRUCT
, *PINFOSTRUCT
;
156 /* structure (in named shared memory) pointing to the above struct */
157 typedef struct _INFOPTR
{
158 PINFOSTRUCT address
; /* pointer to actual memory info */
159 } INFOPTR
, *PINFOPTR
;
161 PINFOSTRUCT meminfo
; /* struct in shared memory holding usage info */
165 /* -----------------------------------------------------------------
167 A brief explanation of the memory allocator :
169 - We store the block's size in front of it. The size implies the nature
170 of the block, and selects a de-allocation scheme..
172 - A note on the memory debugging schemes: logging and online tracking
173 are independent of each other and none, either or both may be used.
175 ----------------------------------------------------------------- */
177 /****************************************************************/
179 /* Allocate a block of memory */
182 void* ft2_malloc( long size
)
188 /* add header size */
189 size
+= sizeof(long);
191 /* Allocate memory accessible from all processes */
192 if (( rc
= SSAllocMem( (PVOID
)&head
, size
, 0 )))
194 COPY( "ft2_malloc: block SSAllocMem failed with rc = " );
201 base
= (void*)(head
+ 1);
203 meminfo
->used
+= size
;
204 if (meminfo
->used
> meminfo
->maxused
)
205 meminfo
->maxused
= meminfo
->used
;
210 /****************************************************************/
212 /* Release a block of memory */
214 int ft2_free( void* block
)
223 head
= ((long*)block
) - 1;
228 COPY( "ft2_free: negative size !!\r\n" );
233 rc
= SSFreeMem( (PVOID
)head
);
236 COPY( "ft2_free: block SSFreeMem failed with rc = " );
242 meminfo
->used
-= size
;
247 /*******************************************************************
249 * Function : TT_Alloc
251 * Description : Allocates memory from the heap buffer.
253 * Input : Size size of the memory to be allocated
254 * P pointer to a buffer pointer
256 * Output : Error code.
258 * NOTE: The newly allocated block should _always_ be zeroed
259 * on return. Many parts of the engine rely on this to
262 ******************************************************************/
264 TT_Error
TT_Alloc( long Size
, void** P
)
268 *P
= ft2_malloc( Size
);
273 return TT_Err_Out_Of_Memory
;
276 /* MEM_Set( *P, 0, Size); */ /* not necessary, SSAllocMem does it */
285 /*******************************************************************
289 * Description : Releases a previously allocated block of memory.
291 * Input : P pointer to memory block
293 * Output : Always SUCCESS.
295 * Note : The pointer must _always_ be set to NULL by this function.
297 ******************************************************************/
299 TT_Error
TT_Free( void** P
)
304 if (ft2_free( *P
)) {
314 /*******************************************************************
316 * Function : TTMemory_Init
318 * Description : Initializes the memory.
320 * Output : Always SUCCESS.
322 ******************************************************************/
324 TT_Error
TTMemory_Init()
331 DosOpen("C:\\FTMEM.LOG", &MemLogHandle
, &Action
, 0, FILE_NORMAL
,
332 OPEN_ACTION_CREATE_IF_NEW
| OPEN_ACTION_REPLACE_IF_EXISTS
,
333 OPEN_FLAGS_NO_CACHE
| OPEN_FLAGS_WRITE_THROUGH
|
334 OPEN_FLAGS_SEQUENTIAL
| OPEN_SHARE_DENYWRITE
| OPEN_ACCESS_WRITEONLY
,
337 COPY("FTMEM Init.\r\n");
343 /* allocate named shared memory and global shared memory */
345 SSAllocMem(&meminfo
, 4096, 0);
346 DosAllocSharedMem((PVOID
*)&memptr
, MEM_NAME
, 4096, fALLOC
);
347 memptr
->address
= meminfo
;
348 meminfo
->signature
= 0x46524545; /* 'FREE' */
349 meminfo
->maxused
= 0;
357 /*******************************************************************
359 * Function : TTMemory_Done
361 * Description : Finalizes memory usage.
363 * Output : Always SUCCESS.
365 ******************************************************************/
367 TT_Error
TTMemory_Done()
369 /* Never called by the font driver (beats me why). We do not
373 DosFreeMem(memptr
); /* free shared memory */
377 COPY("FTMEM Done.\r\n");
379 DosClose(MemLogHandle
);