XBinder  Version 2.9.x
rtxMemory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003-2022 Objective Systems, Inc.
3  *
4  * This software is furnished under a license and may be used and copied
5  * only in accordance with the terms of such license and with the
6  * inclusion of the above copyright notice. This software or any other
7  * copies thereof may not be provided or otherwise made available to any
8  * other person. No title to and ownership of the software is hereby
9  * transferred.
10  *
11  * The information in this software is subject to change without notice
12  * and should not be construed as a commitment by Objective Systems, Inc.
13  *
14  * PROPRIETARY NOTICE
15  *
16  * This software is an unpublished work subject to a confidentiality agreement
17  * and is protected by copyright and trade secret law. Unauthorized copying,
18  * redistribution or other use of this work is prohibited.
19  *
20  * The above notice of copyright on this source code product does not indicate
21  * any actual or intended publication of such source code.
22  *
23  *****************************************************************************/
28 #ifndef __RTXMEMORY_H__
29 #define __RTXMEMORY_H__
30 
31 #include "rtxsrc/rtxContext.h"
32 
33 /*
34  * Uncomment this definition before building the C or C++ run-time
35  * libraries to enable compact memory management. This will have a
36  * smaller code footprint than the standard memory management, however,
37  * the performance may not be as good.
38  */
39 /*#define _MEMCOMPACT*/
40 
41 #define RT_MH_DONTKEEPFREE 0x1
42 #define RT_MH_VALIDATEPTR 0x2
43 #define RT_MH_CHECKHEAP 0x4
44 #define RT_MH_TRACE 0x8
45 #define RT_MH_DIAG 0x10
46 #define RT_MH_DIAG_DEBUG 0x20
47 #define RT_MH_ZEROONFREE 0x40
48 #define RT_MH_ZEROARRAY 0x80
49 #define RT_MH_SYSALLOC 0x100
50 #define RT_MH_TRACE_FREELIST 0x200 /* enable printing of the free element list
51  at certain junctures in the memory
52  management code.
53  Requires building with _MEMTRACE */
54 
55 #define OSRTMH_PROPID_DEFBLKSIZE 1
56 #define OSRTMH_PROPID_SETFLAGS 2
57 #define OSRTMH_PROPID_CLEARFLAGS 3
58 #define OSRTMH_PROPID_KEEPFREEUNITS 4
59 
60 #define OSRTMH_PROPID_USER 10
61 
62 #define OSRTXM_K_MEMBLKSIZ (4*1024)
63 
77 #define OSRTALLOCTYPE(pctxt,type) \
78 (type*) rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type))
79 
86 #define OSRTALLOCTYPEZ(pctxt,type) \
87 (type*) rtxMemHeapAllocZ (&(pctxt)->pMemHeap, sizeof(type))
88 
100 #define OSRTREALLOCARRAY(pctxt,pseqof,type) do {\
101 if (sizeof(type)*(pseqof)->n < (pseqof)->n) return RTERR_NOMEM; \
102 if (((pseqof)->elem = (type*) rtxMemHeapRealloc \
103 (&(pctxt)->pMemHeap, (pseqof)->elem, sizeof(type)*(pseqof)->n)) == 0) \
104 return RTERR_NOMEM; \
105 } while (0)
106 
107 #ifndef _NO_MALLOC
108 #define OSCRTMALLOC0(nbytes) malloc(nbytes)
109 #define OSCRTFREE0(ptr) free(ptr)
110 #else
111 
112 #ifdef _NO_THREADS
113 extern EXTERNRT OSCTXT g_ctxt;
114 
115 #define OSCRTMALLOC0(nbytes) rtxMemAlloc(&g_ctxt,(nbytes))
116 #define OSCRTFREE0(ptr) rtxMemFreePtr(&g_ctxt,(ptr))
117 #else
118 #define OSCRTMALLOC0(nbytes) (void*)0
119 #define OSCRTFREE0(ptr) (void*)0
120 
121 #endif /* _NO_THREADS */
122 #endif /* _NO_MALLOC */
123 
124 #define OSCRTMALLOC rtxMemAlloc
125 #define OSCRTFREE rtxMemFreePtr
126 
127 struct OSRTDList;
128 
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 
133 /* Alias for __cdecl modifier; if __cdecl keyword is not supported,
134  * redefine it as empty macro. */
135 
136 #if !defined(OSCDECL)
137 #if defined(_MSC_VER) || defined(__BORLANDC__)
138 #define OSCDECL __cdecl
139 #else
140 #define OSCDECL
141 #endif
142 #endif /* OSCDECL */
143 
144 EXTERNRT void rtxMemHeapAddRef (void** ppvMemHeap);
145 EXTERNRT void* rtxMemHeapAlloc (void** ppvMemHeap, OSSIZE nbytes);
146 EXTERNRT void* rtxMemHeapAllocZ (void** ppvMemHeap, OSSIZE nbytes);
147 EXTERNRT void* rtxMemHeapSysAlloc (void** ppvMemHeap, OSSIZE nbytes);
148 EXTERNRT void* rtxMemHeapSysAllocZ (void** ppvMemHeap, OSSIZE nbytes);
149 EXTERNRT int rtxMemHeapCheckPtr (void** ppvMemHeap, const void* mem_p);
150 EXTERNRT void rtxMemHeapFreeAll (void** ppvMemHeap);
151 EXTERNRT void rtxMemHeapFreePtr (void** ppvMemHeap, void* mem_p);
152 EXTERNRT void rtxMemHeapSysFreePtr (void** ppvMemHeap, void* mem_p);
153 
154 /* Internal runtime use only.
155  This can only be used with a static memory heap.
156  It will reallocate the last memory block if the given block of the
157  given size appears right at the end of the static memory heap.
158  @param ppvMemHeap the heap
159  @param mem_p Pointer to block to resize
160  @param oldsize The previously allocated size of the block.
161  @param newsize The desired new size of the block.
162 */
163 EXTERNRT void* rtxMemHeapReallocStatic
164 (void** ppvMemHeap, void* mem_p, OSSIZE oldsize, OSSIZE newsize);
165 
166 EXTERNRT void* rtxMemHeapRealloc
167 (void** ppvMemHeap, void* mem_p, OSSIZE nbytes_);
168 
169 EXTERNRT void* rtxMemHeapSysRealloc
170 (void** ppvMemHeap, void* mem_p, OSSIZE nbytes_);
171 
172 EXTERNRT void rtxMemHeapRelease (void** ppvMemHeap);
173 EXTERNRT void rtxMemHeapReset (void** ppvMemHeap);
174 EXTERNRT void rtxMemHeapSetProperty (void** ppvMemHeap,
175  OSUINT32 propId, void* pProp);
176 
177 EXTERNRT void* rtxMemNewArray (OSSIZE nbytes);
178 EXTERNRT void* rtxMemNewArrayZ (OSSIZE nbytes);
179 EXTERNRT void rtxMemDeleteArray (void* mem_p);
180 
181 EXTERNRT void* rtxMemHeapAutoPtrRef (void** ppvMemHeap, void* ptr);
182 EXTERNRT int rtxMemHeapAutoPtrUnref (void** ppvMemHeap, void* ptr);
183 EXTERNRT int rtxMemHeapAutoPtrGetRefCount (void** ppvMemHeap, void* mem_p);
184 
185 EXTERNRT void rtxMemHeapInvalidPtrHook (void** ppvMemHeap, const void* mem_p);
186 
187 EXTERNRT void rtxMemHeapCheck (void **ppvMemHeap, const char* file, int line);
188 
192 EXTERNRT void rtxMemHeapPrint (void **ppvMemHeap);
193 
198 EXTERNRT void rtxMemHeapPrintWithFree (void **ppvMemHeap);
199 
208 EXTERNRT int rtxMemHeapCreate (void** ppvMemHeap);
209 
222 EXTERNRT int rtxMemHeapCreateExt (void** ppvMemHeap,
223  OSMallocFunc malloc_func,
224  OSReallocFunc realloc_func,
225  OSFreeFunc free_func);
226 
244 EXTERNRT int rtxMemStaticHeapCreate
245 (void **ppvMemHeap, void* pmem, OSSIZE memsize) ;
246 
263 EXTERNRT int rtxMemHeapConvertStatic
264 (void** ppvMemHeap, void* pmem, OSSIZE memsize);
265 
266 #if !defined(_ARMTCC) && !defined(__SYMBIAN32__)
267 
282 EXTERNRT void rtxMemSetAllocFuncs (OSMallocFunc malloc_func,
283  OSReallocFunc realloc_func,
284  OSFreeFunc free_func);
285 
286 #endif /* __SYMBIAN32__ */
287 
288 EXTERNRT void rtxMemFreeOpenSeqExt
289 (OSCTXT* pctxt, struct OSRTDList *pElemList);
290 
297 EXTERNRT OSUINT32 rtxMemHeapGetDefBlkSize (OSCTXT* pctxt);
298 
299 #ifndef __SYMBIAN32__
300 
307 EXTERNRT void rtxMemSetDefBlkSize (OSUINT32 blkSize);
308 #endif
309 
316 EXTERNRT OSUINT32 rtxMemGetDefBlkSize (OSVOIDARG);
317 
325 EXTERNRT OSBOOL rtxMemHeapIsEmpty (OSCTXT* pctxt);
326 
335 EXTERNRT OSBOOL rtxMemIsZero (const void* pmem, OSSIZE memsiz);
336 
337 #ifdef _STATIC_HEAP
338 EXTERNRT void rtxMemSetStaticBuf (void* memHeapBuf, OSUINT32 blkSize);
339 #endif
340 
350 #define rtxMemAlloc(pctxt,nbytes) \
351 rtxMemHeapAlloc(&(pctxt)->pMemHeap,nbytes)
352 
363 #define rtxMemSysAlloc(pctxt,nbytes) \
364 rtxMemHeapSysAlloc(&(pctxt)->pMemHeap,nbytes)
365 
375 #define rtxMemAllocZ(pctxt,nbytes) \
376 rtxMemHeapAllocZ(&(pctxt)->pMemHeap,nbytes)
377 
391 #define rtxMemSysAllocZ(pctxt,nbytes) \
392 rtxMemHeapSysAllocZ(&(pctxt)->pMemHeap,nbytes)
393 
394 
409 #define rtxMemRealloc(pctxt,mem_p,nbytes) \
410 rtxMemHeapRealloc(&(pctxt)->pMemHeap, (void*)mem_p, nbytes)
411 
428 #define rtxMemSysRealloc(pctxt,mem_p,nbytes) \
429  rtxMemHeapSysRealloc(&(pctxt)->pMemHeap,(void*)mem_p,nbytes)
430 
442 #define rtxMemFreePtr(pctxt,mem_p) \
443 rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
444 
455 #define rtxMemSysFreePtr(pctxt,mem_p) \
456 rtxMemHeapSysFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
457 
466 EXTERNRT void rtxMemFree (OSCTXT* pctxt);
467 
482 EXTERNRT void rtxMemReset (OSCTXT* pctxt);
483 
493 #define rtxMemAllocType(pctxt,ctype) \
494 (ctype*)rtxMemHeapAlloc(&(pctxt)->pMemHeap,sizeof(ctype))
495 
509 #define rtxMemSysAllocType(pctxt,ctype) \
510 (ctype*)rtxMemHeapSysAlloc(&(pctxt)->pMemHeap,sizeof(ctype))
511 
521 #define rtxMemAllocTypeZ(pctxt,ctype) \
522 (ctype*)rtxMemHeapAllocZ(&(pctxt)->pMemHeap,sizeof(ctype))
523 
537 #define rtxMemSysAllocTypeZ(pctxt,ctype) \
538 (ctype*)rtxMemHeapSysAllocZ(&(pctxt)->pMemHeap,sizeof(ctype))
539 
551 #define rtxMemFreeType(pctxt,mem_p) \
552 rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
553 
565 #define rtxMemSysFreeType(pctxt,mem_p) \
566 rtxMemHeapSysFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
567 
577 #define rtxMemAllocArray(pctxt,n,type) \
578 (type*)rtxMemAllocArray2 (pctxt, n, sizeof(type), 0)
579 
580 EXTERNRT void* rtxMemAllocArray2
581 (OSCTXT* pctxt, OSSIZE numElements, OSSIZE typeSize, OSUINT32 flags);
582 
596 #define rtxMemSysAllocArray(pctxt,n,type) \
597 (type*)rtxMemAllocArray2 (pctxt, n, sizeof(type), RT_MH_SYSALLOC)
598 
608 #define rtxMemAllocArrayZ(pctxt,n,type) \
609 (type*)rtxMemAllocArray2 (pctxt, n, sizeof(type), RT_MH_ZEROARRAY)
610 
622 #define rtxMemFreeArray(pctxt,mem_p) \
623 rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
624 
636 #define rtxMemSysFreeArray(pctxt,mem_p) \
637 rtxMemHeapSysFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
638 
653 #define rtxMemReallocArray(pctxt,mem_p,n,type) \
654 (type*)rtxMemHeapRealloc(&(pctxt)->pMemHeap, (void*)mem_p, sizeof(type)*n)
655 
656 /* Auto-pointer functions */
669 #define rtxMemNewAutoPtr(pctxt,nbytes) \
670 rtxMemHeapAlloc(&(pctxt)->pMemHeap, nbytes)
671 
680 #define rtxMemAutoPtrRef(pctxt,ptr) \
681 rtxMemHeapAutoPtrRef(&(pctxt)->pMemHeap, (void*)(ptr))
682 
693 #define rtxMemAutoPtrUnref(pctxt,ptr) \
694 rtxMemHeapAutoPtrUnref(&(pctxt)->pMemHeap, (void*)(ptr))
695 
704 #define rtxMemAutoPtrGetRefCount(pctxt,ptr) \
705 rtxMemHeapAutoPtrGetRefCount(&(pctxt)->pMemHeap, (void*)(ptr))
706 
715 #define rtxMemCheckPtr(pctxt,mem_p) \
716 rtxMemHeapCheckPtr(&(pctxt)->pMemHeap, (void*)mem_p)
717 
723 #define rtxMemCheck(pctxt) \
724 rtxMemHeapCheck(&(pctxt)->pMemHeap, __FILE__, __LINE__)
725 
731 #define rtxMemPrint(pctxt) \
732 rtxMemHeapPrint(&(pctxt)->pMemHeap)
733 
740 #define rtxMemPrintWithFree(pctxt) \
741 rtxMemHeapPrintWithFree(&(pctxt)->pMemHeap)
742 
750 #define rtxMemSetProperty(pctxt,propId,pProp) \
751 rtxMemHeapSetProperty (&(pctxt)->pMemHeap, propId, pProp)
752 
753 
754 #ifdef __cplusplus
755 }
756 #endif
757 
760 #endif /*__RTXMEMORY_H__*/
EXTERNRT int rtxMemStaticHeapCreate(void **ppvMemHeap, void *pmem, OSSIZE memsize)
This function creates a static memory heap.
EXTERNRT void rtxMemHeapPrintWithFree(void **ppvMemHeap)
Print the same details about the memory heap as rtxMemHeapPrint but add deatils about the free memory...
EXTERNRT OSBOOL rtxMemIsZero(const void *pmem, OSSIZE memsiz)
This helper function determines if an arbitrarily sized block of memory is set to zero...
This is the main list structure.
Definition: rtxDList.h:64
EXTERNRT OSUINT32 rtxMemHeapGetDefBlkSize(OSCTXT *pctxt)
This function returns the actual granularity of memory blocks in the context.
EXTERNRT int rtxMemHeapCreate(void **ppvMemHeap)
This function creates a standard memory heap.
Common run-time context definitions.
EXTERNRT void rtxMemFree(OSCTXT *pctxt)
Free memory associated with a context.
EXTERNRT void rtxMemSetAllocFuncs(OSMallocFunc malloc_func, OSReallocFunc realloc_func, OSFreeFunc free_func)
This function sets the pointers to standard allocation functions.
EXTERNRT int rtxMemHeapCreateExt(void **ppvMemHeap, OSMallocFunc malloc_func, OSReallocFunc realloc_func, OSFreeFunc free_func)
This function creates a standard memory heap and sets the low-level memory functions to the specified...
EXTERNRT void rtxMemHeapPrint(void **ppvMemHeap)
Print details about the memory heap.
EXTERNRT int rtxMemHeapConvertStatic(void **ppvMemHeap, void *pmem, OSSIZE memsize)
This function converts a standard memory heap to a static memory heap.
EXTERNRT OSBOOL rtxMemHeapIsEmpty(OSCTXT *pctxt)
This function determines if the memory heap defined in the give context is empty (i.e.
EXTERNRT void rtxMemReset(OSCTXT *pctxt)
Reset memory associated with a context.
EXTERNRT void rtxMemSetDefBlkSize(OSUINT32 blkSize)
This function sets the minimum size and the granularity of memory blocks for newly created memory hea...
Run-time context structure.
Definition: rtxContext.h:198
EXTERNRT OSUINT32 rtxMemGetDefBlkSize(OSVOIDARG)
This function returns the actual granularity of memory blocks.