1 // This takes care of all memory requests, and frees
2 // It indirects all editor requests for memory
3 // Copyright (C) 2000 Core Technologies.
4
5 // This file is part of e93.
6 //
7 // e93 is free software; you can redistribute it and/or modify
8 // it under the terms of the e93 LICENSE AGREEMENT.
9 //
10 // e93 is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // e93 LICENSE AGREEMENT for more details.
14 //
15 // You should have received a copy of the e93 LICENSE AGREEMENT
16 // along with e93; see the file "LICENSE.TXT".
17
18 #include "includes.h"
19
20 static UINT32
21 numAllocated=0;
22
MResizePtr(void * pointer,UINT32 size)23 void *MResizePtr(void *pointer,UINT32 size)
24 // Resize pointer to size, and return the resized pointer
25 // if there is a problem, do not modify pointer, SetError and return NULL
26 {
27 void
28 *newPointer;
29
30 if((newPointer=(void *)realloc(pointer,(int)size)))
31 {
32 return(newPointer);
33 }
34 else
35 {
36 SetError("Failed to realloc %d bytes",size); // could not allocate memory, so tell user
37 }
38 return(NULL);
39 }
40
MNewPtr(UINT32 size)41 void *MNewPtr(UINT32 size)
42 // Call this to get a contiguous, aligned pointer to memory
43 // If NULL is returned, the request could not be granted
44 // If NULL is returned, GetError may be called to find out what went wrong
45 {
46 void
47 *pointer;
48
49 if((pointer=(void *)malloc((int)size)))
50 {
51 numAllocated++;
52 }
53 else
54 {
55 SetError("Failed to malloc %d bytes",size); // could not allocate memory, so tell user
56 }
57 return(pointer);
58 }
59
MNewPtrClr(UINT32 size)60 void *MNewPtrClr(UINT32 size)
61 // Call this to get a contiguous, aligned pointer to cleared memory
62 // If NULL is returned, the request could not be granted
63 // If NULL is returned, GetError may be called to find out what went wrong
64 {
65 void
66 *pointer;
67
68 if((pointer=(void *)calloc((int)size,1)))
69 {
70 numAllocated++;
71 }
72 else
73 {
74 SetError("Failed to calloc %d bytes",size); // could not allocate memory, so tell user
75 }
76 return(pointer);
77 }
78
MDisposePtr(void * pointer)79 void MDisposePtr(void *pointer)
80 // Dispose of memory allocated by MNewPtr
81 {
82 if(pointer)
83 {
84 numAllocated--;
85 free(pointer);
86 }
87 }
88
MMoveMem(void * source,void * dest,UINT32 numBytes)89 void MMoveMem(void *source,void *dest,UINT32 numBytes)
90 // Move memory from source to dest for numBytes
91 // This must be smart enough to prevent overlapping areas from causing trouble
92 {
93 memmove(dest,source,(int)numBytes);
94 }
95
MGetNumAllocatedPointers()96 UINT32 MGetNumAllocatedPointers()
97 // Return the current number of allocated pointers
98 {
99 return(numAllocated);
100 }
101