MTK使用数组模拟动态内存分配

MTK 使用数组模拟动态内存分配 收藏
在MTK开发中,如果要进行大量的内存分配不是件容易的事情。因此,我们变通的使用这种方法来模拟。可以防止绝大多数的内存问题。已经在项目中实践,放心使用^_^


view plaincopy to clipboardprint?
01.#ifndef _DZD_MEMORY2_H_
02.
03.#define _DZD_MEMORY2_H_
04.
05.
06.
07.int DZD_mem_internal_init (void *mem, unsigned long memsize);
08.
09.void * DZD_mem_internal_malloc (unsigned long size);
10.
11.void DZD_mem_internal_free (void* mem);
12.
13.void DZD_mem_internal_free_all (void);
14.
15.unsigned long DZD_mem_get_size (void *mem);
16.
17.
18.
19.#endif
#ifndef _DZD_MEMORY2_H_

#define _DZD_MEMORY2_H_



int DZD_mem_internal_init (void *mem, unsigned long memsize);

void * DZD_mem_internal_malloc (unsigned long size);

void DZD_mem_internal_free (void* mem);

void DZD_mem_internal_free_all (void);

unsigned long DZD_mem_get_size (void *mem);



#endif


view plaincopy to clipboardprint?
01./*****************************************************************************
02.* filename: DZD_mem.c
03.* author: fulinhai
04.* created date: 9/12, 2005
05.* description: use static array simulates dynamic allocation
06.*
07.*
08.
09.*****************************************************************************/
10.
11.#include "kal_release.h"
12.#include "stack_common.h"
13.#include "stack_msgs.h"
14.#include "stack_config.h"
15.#include "custom_config.h"
16.#include "kal_trace.h"
17.#include "gv.h"
18.#include "tst_def.h"
19.#include "E3E_men.h"
20.//#ifdef __KDZD_INTERNAL_MEMORY_MANAGE__
21.
22.#define DZD_NUM_FREE_LISTS 10
23.#define DZD_CHUNKHEADERSIZE (2 * sizeof(unsigned long))
24.#define DZD_MINCHUNKSIZE sizeof(DZD_chunk_t)
25.#define DZD_MALLOC_ALIGNMENT 4
26.#define DZD_MALLOC_ALIGN_MASK 3
27.#define DZD_MINBLOCKSIZE (12 * DZD_MINCHUNKSIZE)
28.
29.typedef struct DZD_chunk_st{
30. unsigned long prev_size; /* Size of previous chunk. */
31. unsigned long size; /* Size in bytes, including overhead, inuse bit is also stored here. */
32. struct DZD_chunk_st* fwd; /* Double links -- used only if free. */
33. struct DZD_chunk_st* bck;
34.} DZD_chunk_t;
35.typedef DZD_chunk_t* DZD_chunkptr;
36.
37.typedef struct {
38. unsigned char *baseptr;
39. DZD_chunkptr firstchunk;
40. DZD_chunkptr lastchunk;
41. DZD_chunkptr freelists[DZD_NUM_FREE_LISTS];
42.} DZD_mem_internal_t;
43.
44.DZD_mem_internal_t DZD_mem_internal;
45.#define DZD_INT_GLOBAL_MEM_SIZE (1024*300)
46./*
47.* We use segregated free lists, wit

h separate lists for
48.* chunks of different sizes. Currently, we use sizes that are
49.* powers of two.
50.* In list number n is kept all the free chunks whose size is
51.* strictly less than maxsizes[n].
52.*/
53.
54.const static unsigned long maxsizes[DZD_NUM_FREE_LISTS] = {
55. 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 0x8FFFFFFF
56.};
57./*
58.* List operations.
59.*/
60.#define DZD_remove_from_list(p) \
61. (p)->fwd->bck = (p)->bck; \
62. (p)->bck->fwd = (p)->fwd;
63.
64.
65.#define DZD_add_to_list(l, p) \
66. (p)->fwd = (l)->fwd; \
67. (p)->bck = l; \
68. (l)->fwd->bck = p; \
69. (l)->fwd = p;
70.
71.#define DZD_set_hd1(p, v) ((p)->prev_size = (unsigned long)(v))
72.#define DZD_set_hd2(p, v) ((p)->size = (unsigned long)(v))
73.#define DZD_request2size(req) \
74.(((unsigned long)((req) + DZD_CHUNKHEADERSIZE + DZD_MALLOC_ALIGN_MASK) < \
75. (unsigned long)(DZD_MINCHUNKSIZE + DZD_MALLOC_ALIGN_MASK)) ? DZD_MINCHUNKSIZE : \
76. (unsigned long)(((req) + DZD_CHUNKHEADERSIZE + DZD_MALLOC_ALIGN_MASK) & ~(DZD_MALLOC_ALIGN_MASK)))
77.#define DZD_chunksize(p) (((p)->size & ~0x01) >> 1)
78.
79./* The next and previous links */
80.#define DZD_prevchunk(p) ((DZD_chunkptr)(((unsigned char *)(p)) - p->prev_size))
81.#define DZD_nextchunk(p) ((DZD_chunkptr)(((unsigned char *)(p)) + DZD_chunksize (p)))
82.
83./* The IN USE bit */
84.#define DZD_chunk_isfree(p) ((((DZD_chunkptr)(p))->size & 0x01) == 0)
85.#define DZD_chunk_inuse(p) ((((DZD_chunkptr)(p))->size & 0x01) == 1)
86./* Conversion from chunk headers to user pointers, and back */
87.#define DZD_chunk2mem(p) ((void*)((unsigned char *)(p) + DZD_CHUNKHEADERSIZE))
88.#define DZD_mem2chunk(mem) ((DZD_chunkptr)((unsigned char *)(mem) - DZD_CHUNKHEADERSIZE))
89.
90./*
91.* Return the number of the list that a chunk of size "n"
92.* belongs to.
93.*/
94.int DZD_list_idx (unsigned long n)
95.{
96. int l;
97.
98. for (l = 0; maxsizes[l] <= n; l++);
99.
100. return l;
101.}
102.
103./****************************************************************
104.* function name: DZD_mem_internal_init
105.* parameter:
106.* mem static memory
107.* memsize size of the static memory
108.* description:
109.* Initialize the internal memory allocation library.
110.* the memory library, the second is a pointer to a block
111.* of memory and the third is the size of the memory area.
112.* Returns 0 on success, and -1 otherwise.
113.****************************************************************/
114.int DZD_mem_internal_init (void *mem, unsigned long memsize)
115.{
116. DZD_mem_internal_t* mod_mem;
117.

DZD_chunkptr p;
118. unsigned long size;
119. short i;
120.
121. mod_mem = &DZD_mem_internal;
122.
123. /* Truncate to nearest multiple of the alignment. */
124. memsize &= ~DZD_MALLOC_ALIGN_MASK;//使内存以四对齐
125. if ((mem == NULL) || (memsize < DZD_MINBLOCKSIZE) || (memsize > 0x7FFFFFFF))
126. {
127. mod_mem->baseptr = NULL;
128. return -1;
129. }
130. mod_mem->baseptr = (unsigned char*)mem;
131.
132. /* For each of the lists of free chunks, we need one chunk to
133. * serve as list header. We mark each one as "in use" to avoid
134. * having it merged with an adjacent chunk on freeing. */
135. for (i = 0; i < DZD_NUM_FREE_LISTS; i++) {
136. p = (DZD_chunkptr)(mod_mem->baseptr + i * DZD_MINCHUNKSIZE);
137. DZD_set_hd1 (p, 0);
138. DZD_set_hd2 (p, (DZD_MINCHUNKSIZE << 1) | 0x01);
139. p->fwd = p;
140. p->bck = p;
141. mod_mem->freelists[i] = p;
142. }
143.
144. /* This chunk starts out on the appropriate free list and
145. * contains all of the available memory. */
146. mod_mem->firstchunk = (DZD_chunkptr)(mod_mem->baseptr + DZD_NUM_FREE_LISTS * DZD_MINCHUNKSIZE);
147. DZD_set_hd1 (mod_mem->firstchunk, DZD_MINCHUNKSIZE);
148. size = memsize - ((DZD_NUM_FREE_LISTS + 1) * DZD_MINCHUNKSIZE);
149. DZD_set_hd2 (mod_mem->firstchunk, size << 1);
150. DZD_add_to_list (mod_mem->freelists[DZD_list_idx (size)], mod_mem->firstchunk);
151.
152. /* The last chunk is never placed on any list; it is marked "in use",
153. * and simply acts as a sentry element, when scanning all the chunks. */
154. mod_mem->lastchunk = (DZD_chunkptr)(((unsigned char *)(mod_mem->baseptr)) + memsize - DZD_MINCHUNKSIZE);
155. DZD_set_hd1 (mod_mem->lastchunk, size);
156. DZD_set_hd2 (mod_mem->lastchunk, (DZD_MINCHUNKSIZE << 1) | 0x01);
157.
158. return 0;
159.}
/*****************************************************************************
* filename: DZD_mem.c
* author: fulinhai
* created date: 9/12, 2005
* description: use static array simulates dynamic allocation
*
*

*****************************************************************************/

#include "kal_release.h"
#include "stack_common.h"
#include "stack_msgs.h"
#include "stack_config.h"
#include "custom_config.h"
#include "kal_trace.h"
#include "gv.h"
#include "tst_def.h"
#include "E3E_men.h"
//#ifdef __KDZD_INTERNAL_MEMORY_MANAGE__

#define DZD_NUM_FREE_LISTS 10
#define DZD_CHUNKHEADERSIZE (2 * sizeof(unsigned

long))
#define DZD_MINCHUNKSIZE sizeof(DZD_chunk_t)
#define DZD_MALLOC_ALIGNMENT 4
#define DZD_MALLOC_ALIGN_MASK 3
#define DZD_MINBLOCKSIZE (12 * DZD_MINCHUNKSIZE)

typedef struct DZD_chunk_st{
unsigned long prev_size; /* Size of previous chunk. */
unsigned long size; /* Size in bytes, including overhead, inuse bit is also stored here. */
struct DZD_chunk_st* fwd; /* Double links -- used only if free. */
struct DZD_chunk_st* bck;
} DZD_chunk_t;
typedef DZD_chunk_t* DZD_chunkptr;

typedef struct {
unsigned char *baseptr;
DZD_chunkptr firstchunk;
DZD_chunkptr lastchunk;
DZD_chunkptr freelists[DZD_NUM_FREE_LISTS];
} DZD_mem_internal_t;

DZD_mem_internal_t DZD_mem_internal;
#define DZD_INT_GLOBAL_MEM_SIZE (1024*300)
/*
* We use segregated free lists, with separate lists for
* chunks of different sizes. Currently, we use sizes that are
* powers of two.
* In list number n is kept all the free chunks whose size is
* strictly less than maxsizes[n].
*/

const static unsigned long maxsizes[DZD_NUM_FREE_LISTS] = {
16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 0x8FFFFFFF
};
/*
* List operations.
*/
#define DZD_remove_from_list(p) \
(p)->fwd->bck = (p)->bck; \
(p)->bck->fwd = (p)->fwd;


#define DZD_add_to_list(l, p) \
(p)->fwd = (l)->fwd; \
(p)->bck = l; \
(l)->fwd->bck = p; \
(l)->fwd = p;

#define DZD_set_hd1(p, v) ((p)->prev_size = (unsigned long)(v))
#define DZD_set_hd2(p, v) ((p)->size = (unsigned long)(v))
#define DZD_request2size(req) \
(((unsigned long)((req) + DZD_CHUNKHEADERSIZE + DZD_MALLOC_ALIGN_MASK) < \
(unsigned long)(DZD_MINCHUNKSIZE + DZD_MALLOC_ALIGN_MASK)) ? DZD_MINCHUNKSIZE : \
(unsigned long)(((req) + DZD_CHUNKHEADERSIZE + DZD_MALLOC_ALIGN_MASK) & ~(DZD_MALLOC_ALIGN_MASK)))
#define DZD_chunksize(p) (((p)->size & ~0x01) >> 1)

/* The next and previous links */
#define DZD_prevchunk(p) ((DZD_chunkptr)(((unsigned char *)(p)) - p->prev_size))
#define DZD_nextchunk(p) ((DZD_chunkptr)(((unsigned char *)(p)) + DZD_chunksize (p)))

/* The IN USE bit */
#define DZD_chunk_isfree(p) ((((DZD_chunkptr)(p))->size & 0x01) == 0)
#define DZD_chunk_inuse(p) ((((DZD_chunkptr)(p))->size & 0x01) == 1)
/* Conversion from chunk headers to user pointers, and back */
#define DZD_chunk2mem(p) ((void*)((unsigned char *)(p) + DZD_CHUNKHEADERSIZE))
#define DZD_mem2chunk(mem) ((DZD_chunkptr)((unsigned char *)(mem) - DZD_CHUNKHEADERSIZE))

/*
* Return the number of the list that a chunk of size "n"
* belongs to.
*/
int DZD_list_idx (unsigned long n)
{
int l;

for (l = 0; maxsizes[l] <= n; l++);

return l;
}

/****************************************************************
* function name: DZD_mem_internal_init
* parameter:
* mem static memory
* memsize size of the static memory
* de

scription:
* Initialize the internal memory allocation library.
* the memory library, the second is a pointer to a block
* of memory and the third is the size of the memory area.
* Returns 0 on success, and -1 otherwise.
****************************************************************/
int DZD_mem_internal_init (void *mem, unsigned long memsize)
{
DZD_mem_internal_t* mod_mem;
DZD_chunkptr p;
unsigned long size;
short i;

mod_mem = &DZD_mem_internal;

/* Truncate to nearest multiple of the alignment. */
memsize &= ~DZD_MALLOC_ALIGN_MASK;//使内存以四对齐
if ((mem == NULL) || (memsize < DZD_MINBLOCKSIZE) || (memsize > 0x7FFFFFFF))
{
mod_mem->baseptr = NULL;
return -1;
}
mod_mem->baseptr = (unsigned char*)mem;

/* For each of the lists of free chunks, we need one chunk to
* serve as list header. We mark each one as "in use" to avoid
* having it merged with an adjacent chunk on freeing. */
for (i = 0; i < DZD_NUM_FREE_LISTS; i++) {
p = (DZD_chunkptr)(mod_mem->baseptr + i * DZD_MINCHUNKSIZE);
DZD_set_hd1 (p, 0);
DZD_set_hd2 (p, (DZD_MINCHUNKSIZE << 1) | 0x01);
p->fwd = p;
p->bck = p;
mod_mem->freelists[i] = p;
}

/* This chunk starts out on the appropriate free list and
* contains all of the available memory. */
mod_mem->firstchunk = (DZD_chunkptr)(mod_mem->baseptr + DZD_NUM_FREE_LISTS * DZD_MINCHUNKSIZE);
DZD_set_hd1 (mod_mem->firstchunk, DZD_MINCHUNKSIZE);
size = memsize - ((DZD_NUM_FREE_LISTS + 1) * DZD_MINCHUNKSIZE);
DZD_set_hd2 (mod_mem->firstchunk, size << 1);
DZD_add_to_list (mod_mem->freelists[DZD_list_idx (size)], mod_mem->firstchunk);

/* The last chunk is never placed on any list; it is marked "in use",
* and simply acts as a sentry element, when scanning all the chunks. */
mod_mem->lastchunk = (DZD_chunkptr)(((unsigned char *)(mod_mem->baseptr)) + memsize - DZD_MINCHUNKSIZE);
DZD_set_hd1 (mod_mem->lastchunk, size);
DZD_set_hd2 (mod_mem->lastchunk, (DZD_MINCHUNKSIZE << 1) | 0x01);

return 0;
}

view plaincopy to clipboardprint?
01./****************************************************************
02.* function name: DZD_mem_internal_init
03.* parameter:
04.* mem static memory
05.* memsize size of the static memory
06.* description:
07.* Allocate and return a pointer to a memory area of at least
08.* the indicated size. The allocated block of memory will be aligned
09.*

on a 4-byte boundary.
10.* Returns 0 if allocation fails.
11.****************************************************************/
12.void * DZD_mem_internal_malloc (unsigned long size)
13.{
14. DZD_mem_internal_t* mod_mem;
15. DZD_chunkptr p = 0, ptmp;
16. unsigned long nb;
17. unsigned long sz = DZD_INT_GLOBAL_MEM_SIZE;
18. unsigned long remsize;
19. int i;
20.
21. mod_mem = &(DZD_mem_internal);
22.
23. if (mod_mem->baseptr == NULL)
24. return 0;
25. /* We add space for our overhead (4 bytes) plus round to nearest
26. * larger multiple of 4, plus never allocate a chunk less than 8 bytes. */
27. nb = DZD_request2size (size);
28.
29. /* Check all relevant free lists, until a non-empty one is found. */
30. for (i = DZD_list_idx (nb); i < DZD_NUM_FREE_LISTS; i++)
31. {
32. DZD_chunkptr freelist = mod_mem->freelists[i];
33.
34. /* Search the entire list, select chunk with closest fit */
35. for (ptmp = freelist->fwd; ptmp != freelist; ptmp = ptmp->fwd)
36. {
37. unsigned long tmpsz = DZD_chunksize (ptmp);
38.
39. if (tmpsz == nb)
40. {
41. /* Exact fit: no need to search any further. */
42. p = ptmp;
43. sz = tmpsz;
44. goto found;
45. }
46. else if (tmpsz > nb)
47. {
48. /* Chunk is large enough */
49. if (tmpsz < sz)
50. {
51. /* This is the best so far. */
52. p = ptmp;
53. sz = tmpsz;
54. }
55. }
56. }
57. if (p != 0)
58. goto found;
59. }
60. /* Searched all lists, but found no large enough chunk */
61. return 0;
62.
63. found:
64. /* We have found a large enough chunk, namely "p" of size "sz". */
65. DZD_remove_from_list (p);
66. remsize = sz - nb;
67.
68. if (remsize >= DZD_MINCHUNKSIZE)
69. {
70. /* The remainder is large enough to become a separate chunk */
71. DZD_chunkptr q, next;
72. DZD_chunkptr l;
73.
74. sz = nb;
75.

/* "q" will be the new chunk */
76. q = (DZD_chunkptr)((unsigned char *)p + sz);
77. DZD_set_hd2 (q, remsize << 1);
78. DZD_set_hd1 (q, nb);
79. next = DZD_nextchunk (q);
80. next->prev_size = remsize;
81.
82. l = mod_mem->freelists[DZD_list_idx (remsize)];
83. DZD_add_to_list (l, q);
84. }
85. DZD_set_hd2 (p, (sz << 1) | 0x01);
86. return DZD_chunk2mem (p);
87.}
88.
89./*******************************************************************
90.* function name: DZD_mem_internal_free
91.* parameter:
92.* mem allocated memory
93.* description:
94.* Free a memory area previously allocated with msf_mem_internal_malloc.
95.* Calling this routine with 'mem' equal to 0 has no effect.
96.******************************************************************/
97.void DZD_mem_internal_free (void* mem)
98.{
99. DZD_mem_internal_t* mod_mem;
100. DZD_chunkptr p; /* chunk corresponding to mem */
101. unsigned long sz; /* its size */
102. DZD_chunkptr next; /* next adjacent chunk */
103. DZD_chunkptr prev; /* previous adjacent chunk */
104. DZD_chunkptr l;
105.
106. mod_mem = &(DZD_mem_internal);
107. if (mem == 0 || mod_mem->baseptr == NULL)
108. return;
109.
110. p = DZD_mem2chunk (mem);
111.
112. if (DZD_chunk_isfree (p))
113. return;
114. sz = DZD_chunksize (p);
115. prev = DZD_prevchunk (p);
116. next = DZD_nextchunk (p);
117.
118. if (DZD_chunk_isfree (prev))
119. { /* Join with previous chunk */
120. sz += DZD_chunksize (prev);
121. p = prev;
122. DZD_remove_from_list (prev);
123. }
124. if (DZD_chunk_isfree (next))
125. { /* Join with next chunk */
126. sz += DZD_chunksize (next);
127. DZD_remove_from_list (next);
128. }
129. DZD_set_hd2 (p, sz << 1);
130. next = DZD_nextchunk (p);
131. next->prev_size = sz;
132.
133. l = mod_mem->freelists [DZD_list_idx (sz)];
134. DZD_add_to_list (l, p);
135.}
136.
137./*******************************************************************
138.* function name: DZD_mem_internal_free_all
139.* parameter:
140.* none
141.* description:
142.* Free all memory areas allocated by a module, by doing a new Initialize.
143.***

***************************************************************/
144.void DZD_mem_internal_free_all (void)
145.{
146. void* mem;
147. unsigned long size = 0;
148.
149. mem = DZD_mem_internal.baseptr;
150.// kal_prompt_trace(MOD_MMI, "Free All");
151.
152. if (mem != NULL)
153. {
154. size = (unsigned long)(((unsigned char*)(DZD_mem_https://www.360docs.net/doc/4216872880.html,stchunk)) + DZD_MINCHUNKSIZE - (unsigned char*)mem);
155. DZD_mem_internal_init (mem, size);
156. }
157.}
158.
159./*******************************************************************
160.* function name: DZD_mem_get_size
161.* parameter:
162.* none
163.* description:
164.* Return the size of an allocated memory area.
165.* The reported size includes the overhead used by the memory allocation
166.* system.
167.******************************************************************/
168.unsigned long DZD_mem_get_size (void *mem)
169.{
170. DZD_chunkptr p; /* chunk corresponding to mem */
171. unsigned long size;
172.
173. p = DZD_mem2chunk (mem);
174. size = DZD_chunksize (p);
175.
176. //ASSERT(size < DZD_INT_GLOBAL_MEM_SIZE);
177. return size;
178.}
179.//#endif
/****************************************************************
* function name: DZD_mem_internal_init
* parameter:
* mem static memory
* memsize size of the static memory
* description:
* Allocate and return a pointer to a memory area of at least
* the indicated size. The allocated block of memory will be aligned
* on a 4-byte boundary.
* Returns 0 if allocation fails.
****************************************************************/
void * DZD_mem_internal_malloc (unsigned long size)
{
DZD_mem_internal_t* mod_mem;
DZD_chunkptr p = 0, ptmp;
unsigned long nb;
unsigned long sz = DZD_INT_GLOBAL_MEM_SIZE;
unsigned long remsize;
int i;

mod_mem = &(DZD_mem_internal);

if (mod_mem->baseptr == NULL)
return 0;
/* We add space for our overhead (4 bytes) plus round to nearest
* larger multiple of 4, plus never allocate a chunk less than 8 bytes. */
nb = DZD_request2size (size);

/* Check all relevant free lists, until a non-empty one is found. */
for (i = DZD_list_idx (nb); i < DZD_NUM_FREE_LISTS; i++)
{
DZD_chunkptr freelist = mod_mem->freelists[i];

/* Search the entire list, select chunk with closest fit */
for (ptmp = freel

ist->fwd; ptmp != freelist; ptmp = ptmp->fwd)
{
unsigned long tmpsz = DZD_chunksize (ptmp);

if (tmpsz == nb)
{
/* Exact fit: no need to search any further. */
p = ptmp;
sz = tmpsz;
goto found;
}
else if (tmpsz > nb)
{
/* Chunk is large enough */
if (tmpsz < sz)
{
/* This is the best so far. */
p = ptmp;
sz = tmpsz;
}
}
}
if (p != 0)
goto found;
}
/* Searched all lists, but found no large enough chunk */
return 0;

found:
/* We have found a large enough chunk, namely "p" of size "sz". */
DZD_remove_from_list (p);
remsize = sz - nb;

if (remsize >= DZD_MINCHUNKSIZE)
{
/* The remainder is large enough to become a separate chunk */
DZD_chunkptr q, next;
DZD_chunkptr l;

sz = nb;
/* "q" will be the new chunk */
q = (DZD_chunkptr)((unsigned char *)p + sz);
DZD_set_hd2 (q, remsize << 1);
DZD_set_hd1 (q, nb);
next = DZD_nextchunk (q);
next->prev_size = remsize;

l = mod_mem->freelists[DZD_list_idx (remsize)];
DZD_add_to_list (l, q);
}
DZD_set_hd2 (p, (sz << 1) | 0x01);
return DZD_chunk2mem (p);
}

/*******************************************************************
* function name: DZD_mem_internal_free
* parameter:
* mem allocated memory
* description:
* Free a memory area previously allocated with msf_mem_internal_malloc.
* Calling this routine with 'mem' equal to 0 has no effect.
******************************************************************/
void DZD_mem_internal_free (void* mem)
{
DZD_mem_internal_t* mod_mem;
DZD_chunkptr p; /* chunk corresponding to mem */
unsigned long sz; /* its size */
DZD_chunkptr next; /* next adjacent chunk */
DZD_chunkptr prev; /* previous adjacent chunk */
DZD_chunkptr l;

mod_mem = &(DZD_mem_internal);
if (mem == 0 || mod_mem->baseptr == NULL)
retu

rn;

p = DZD_mem2chunk (mem);

if (DZD_chunk_isfree (p))
return;
sz = DZD_chunksize (p);
prev = DZD_prevchunk (p);
next = DZD_nextchunk (p);

if (DZD_chunk_isfree (prev))
{ /* Join with previous chunk */
sz += DZD_chunksize (prev);
p = prev;
DZD_remove_from_list (prev);
}
if (DZD_chunk_isfree (next))
{ /* Join with next chunk */
sz += DZD_chunksize (next);
DZD_remove_from_list (next);
}
DZD_set_hd2 (p, sz << 1);
next = DZD_nextchunk (p);
next->prev_size = sz;

l = mod_mem->freelists [DZD_list_idx (sz)];
DZD_add_to_list (l, p);
}

/*******************************************************************
* function name: DZD_mem_internal_free_all
* parameter:
* none
* description:
* Free all memory areas allocated by a module, by doing a new Initialize.
******************************************************************/
void DZD_mem_internal_free_all (void)
{
void* mem;
unsigned long size = 0;

mem = DZD_mem_internal.baseptr;
// kal_prompt_trace(MOD_MMI, "Free All");

if (mem != NULL)
{
size = (unsigned long)(((unsigned char*)(DZD_mem_https://www.360docs.net/doc/4216872880.html,stchunk)) + DZD_MINCHUNKSIZE - (unsigned char*)mem);
DZD_mem_internal_init (mem, size);
}
}

/*******************************************************************
* function name: DZD_mem_get_size
* parameter:
* none
* description:
* Return the size of an allocated memory area.
* The reported size includes the overhead used by the memory allocation
* system.
******************************************************************/
unsigned long DZD_mem_get_size (void *mem)
{
DZD_chunkptr p; /* chunk corresponding to mem */
unsigned long size;

p = DZD_mem2chunk (mem);
size = DZD_chunksize (p);

//ASSERT(size < DZD_INT_GLOBAL_MEM_SIZE);
return size;
}
//#endif

使用的时候很简单

p= (U16*)DZD_mem_internal_malloc(sizeof(U16)*MAX_COURSE_FILENAME_LEN);

......
DZD_mem_internal_free(p);


相关文档
最新文档