Skip to main content

the memory management in CLDC of "phoneME Feature software"

1 reply [Last post]
xilongpei
Offline
Joined: 2007-02-24
Points: 0

the following is a new OSMemory_***.cpp program suit for some environment that has no page memory API support.
------------------------------------------------------------

/*
* OsMemory_elastos.cpp:
*/

#include "incls/_precompiled.incl"
#include "incls/_OsMemory_elastos.cpp.incl"

#if !ENABLE_PCSL

#ifdef __cplusplus
extern "C" {
#endif

typedef struct tagChunksMan {
void *refPtr;
void *alignedPtr;
} CHUNKS_MAN;

static CHUNKS_MAN chunksMan[] = { {NULL,NULL}, {NULL,NULL}, {NULL,NULL}, {NULL,NULL}, {NULL,NULL}};
#define MAX_NUM_CHUNKS sizeof(chunksMan)/sizeof(CHUNKS_MAN)

void *OsMemory_allocate(size_t size) {
return malloc(size);
}

void OsMemory_free(void *p) {
free(p);
}

address OsMemory_allocate_chunk(size_t initial_size,
size_t max_size, size_t alignment)
{
int i;
address chunk;

for(i = 0; i < MAX_NUM_CHUNKS && NULL != chunksMan[i].refPtr; i++ );
if( i >= MAX_NUM_CHUNKS ) {
return 0;
}

chunk = (address)malloc( max_size+alignment );
if( 0 == chunk ) {
return 0;
}
chunksMan[i].refPtr = (void *)chunk;
chunksMan[i].alignedPtr = chunk + (alignment - (unsigned long)chunk % alignment);

GUARANTEE(((int)chunksMan[i].alignedPtr) % alignment == 0, "must be aligned");

return (address)chunksMan[i].alignedPtr;

}

// Expand or shrink a chunk returned by allocate_chunk().
// The chunk is never moved.
//
// Returns 0 if fail to expand (shrink will always succeed). Returns old
// size if successful.
size_t OsMemory_adjust_chunk(address chunk_ptr, size_t new_committed_size) {
/**
MEMORY_BASIC_INFORMATION info;
VirtualQuery((LPVOID)chunk_ptr, &info, sizeof(info));
size_t old_committed_size = info.RegionSize;

if (new_committed_size > old_committed_size) {
if (VirtualAlloc((LPVOID)chunk_ptr, new_committed_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE) == NULL) {
AZZERT_ONLY(int err = GetLastError());
return 0;
} else {
return old_committed_size;
}
} else {
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);

new_committed_size = align_size_up(new_committed_size,sys_info.dwPageSize);
if (new_committed_size < old_committed_size) {
address addr_to_free = chunk_ptr + new_committed_size;
size_t bytes_to_free = old_committed_size - new_committed_size;
//VirtualFree((LPVOID)addr_to_free, bytes_to_free, MEM_DECOMMIT);
}
return old_committed_size;
}
*/
return 0;
}

void OsMemory_free_chunk(address chunk_ptr) {
int i;

if( NULL == chunk_ptr )
return;

for(i = 0; i < MAX_NUM_CHUNKS && (address)chunksMan[i].alignedPtr != chunk_ptr; i++ );
if( i >= MAX_NUM_CHUNKS ) {
return;
}

free( chunksMan[i].refPtr );
chunksMan[i].refPtr = NULL;
chunksMan[i].alignedPtr = NULL;

}

#ifdef __cplusplus
}
#endif

#endif // !ENABLE_PCSL

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
xilongpei
Offline
Joined: 2007-02-24
Points: 0

this is some problem on this BBS.
the program that I posted cann't be displayed correctly.
----------------------------------------------------
void OsMemory_free_chunk(address chunk_ptr) {
int i;

if( NULL == chunk_ptr )
return;

for(i = 0; i < MAX_NUM_CHUNKS && (address)chunksMan[i].alignedPtr != chunk_ptr; i++ );
if( i >= MAX_NUM_CHUNKS ) {
return;
}

free( chunksMan[i].refPtr );
chunksMan[i].refPtr = NULL;
chunksMan[i].alignedPtr = NULL;

}