LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2010, 09:20 PM   #1
mysterious88
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Smile How do I implement memory manager get and put (My malloc() )


I want to implement my own memory manager so far I have got..

void memory_init (memorym_t *MM, int hm, int sz)

function to initialize my pool of memory.

I want to implement
memorym_put(memorym_t *MM, void *chunk) and

MM->M_Chunk_Used[i] = 0;

MM->M_Chunk[i] = NULL;

void * mm_get(memorym_t *MM)
int temp, i;
temp = MM->UsedChunk;
temp+=1;
MM->M_Chunk[temp] = NULL;
MM->M_Chunk_Used[temp] = 1;
MM->UsedChunk = temp;

my strunct is
typedef struct {
void *M_Chunk;
int *M_Chunk_Used;
int UsedChunk
} memorym_t;

I am little lost, its compiling without any error but bad object file. You thoughts will be appreciated
 
Old 11-15-2010, 09:53 PM   #2
mac.tieu
Member
 
Registered: Jan 2010
Location: Vietnam
Distribution: Arch
Posts: 65

Rep: Reputation: 22
Your problem may be lied at 'mm_get' function. You should increase 'UsedChunk' after usage like but not before like:

Code:
void * mm_get(memorym_t *MM)
	int temp, i;
	temp = MM->UsedChunk;
	/*temp+=1;*/
	MM->M_Chunk[temp] = NULL; 
	MM->M_Chunk_Used[temp] = 1;
	MM->UsedChunk++; /*= temp;*/
Quote:
Originally Posted by mysterious88 View Post
I want to implement my own memory manager so far I have got..

void memory_init (memorym_t *MM, int hm, int sz)

function to initialize my pool of memory.

I want to implement
memorym_put(memorym_t *MM, void *chunk) and

MM->M_Chunk_Used[i] = 0;

MM->M_Chunk[i] = NULL;

void * mm_get(memorym_t *MM)
int temp, i;
temp = MM->UsedChunk;
temp+=1;
MM->M_Chunk[temp] = NULL;
MM->M_Chunk_Used[temp] = 1;
MM->UsedChunk = temp;

my strunct is
typedef struct {
void *M_Chunk;
int *M_Chunk_Used;
int UsedChunk
} memorym_t;

I am little lost, its compiling without any error but bad object file. You thoughts will be appreciated
 
Old 11-16-2010, 12:44 AM   #3
mysterious88
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
I am confused with put_memorym in memory manager

It is supposed to give back chunk to memory manager, don’t free it though!
void memorym_put (memorym_t *MM, void *chunk);

//Set used status to 0
MM->M_Chunk_Used[i] = 0;
//Set mem_chunk to NULL to blank out space
MM->M_Chunk[i] = NULL;

How do I go about giving back to memory manager. If I use for loop with what I should compare I am totally lost. (about the upper bound).

I don't know if my whole approach is wrong. Any idea suggestion will be appreciated.
 
Old 11-16-2010, 09:45 AM   #4
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
I suggest using the approach of a LIFO (stack).

Psuedo-code datastructure:

Code:
structure MemoryPool
    ptr MemoryBlock pFreeListHead;
    integer nBlockSize
    integer nTotalBlocksCount
    integer nFreeBlocksCount

    long integer nAllocations
    long integer nDeletions

    ptr void pMemoryStart
    ptr void pMemoryEnd
    
structure MemoryBlock
    hex Guardband
    integer Size
    ptr MemoryBlock next
    ptr MemoryPool pPoolOwner
    8-bit State { ALLOC, FREE, FROM_HEAP }
    hex Guardband
And the psuedo-code for using:
Code:
initialize_pool(ptr structure MemoryPool pool, ... /* configuration parameters (size, count) */)
    clear pool
    internally allocate enough memory to hold (count number of memory blocks + count * size)
    set pool pMemoryStart and pMemoryEnd
    starting at the last byte of memory and for each count:
        advance size bytes earlier, and size of MemoryBlock bytes earlier
        treat the current position as a MemoryBlock and initialize
        set the state to FREE
        set MemoryBlock next to pool pFreeListHead
        set pool pFreeListHead to current MemoryBlock

ptr void allocate_buffer(ptr structure MemoryPool pool)
    ptr MemoryBlock buffer_ equals pool pFreeListHead
    if(buffer_)
        set buffer_ state to ALLOC
        set pool pFreeListHead to buffer_ next
        return ptr to the bytes just after the buffer_ header
    /* else it is up to you to handle the case of no more buffers */

delete_buffer(ptr structure MemoryPool pool, ptr void Buffer)
    /* you merely have to ensure that the Buffer header is correct, and pFreeListHead of pool is updated correctly */
It's up to you to actually write the code, and also to think about synchronization issues (and how to solve them).
You can also think about where the trade-offs are when compared to a dynamic heap - performance, memory usage, etc.

As-written, your heap implementation doesn't sound like a good idea. Compare it with the one I've outlined above.

-Aaron
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C, Malloc, and Memory: Just Curious CoderMan Programming 4 01-20-2010 09:17 PM
get a 'n' byte aligned memory from malloc kapsikum Programming 1 05-30-2008 01:51 PM
malloc() memory corruption (C++) Coxynelle Programming 1 01-17-2007 04:07 PM
malloc() and system memory spaceman27 Programming 8 04-25-2005 09:58 AM
Out Of Memory - A caring malloc? cyent Programming 3 09-18-2002 09:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration