LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-01-2010, 01:53 AM   #1
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Rep: Reputation: 9
data structure to manage memory


I am working on a problem (re inventing the wheel for learning)
I have a mxn matric (which is my simplified way of saying it is RAM with bytes on it)
Some of the locations on this metric is filled with some data and some places are empty.
The mxn are very big numbers in size.
I am trying to make a program so that if a system call wants to write some thing on empty locations on this mxn metric it should be able to do so without any problem.
The thing which I want to understand or logic of a data structure is what data structure do you people feel should I be maintaining so that I can allocate the requested space immediately from the above mxn matric when some system call requests for some (k) number of locations from above metrics.


The logic initially I thought was to maintain a hashtable

1bytes requested----------> location 1,location 2,location 3.........location n
2bytes requested----------> location 1,location 2,location 3.........location n
3bytes requested----------> location 1,location 2,location 3.........location n
4bytes requested----------> location 1,location 2,location 3.........location n
.
.
.
nbytes requested----------> location 1,location 2,location 3.........location n

but the problem with above logic is size of the pointers where I will be writing this problem is unsigned 64 byte.So to know location of one free byte if I am maintaining one pointer of type u64 this is not a feasible solution.
Can you people suggest me some logic,idea,algorithm for the same.
 
Old 10-01-2010, 03:19 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

There are many ways to implement this.

http://en.wikipedia.org/wiki/Sparse_matrix
 
Old 10-01-2010, 03:26 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Why are you using a matrix rather than a single dimension array?
A mxn matrix could be useful if you are allocating memory in blocks of n bytes and so you have m available to allocate.

Do you want your memory model have the ability to reallocate itself, this allowing it to shuffle the memory around and so remove gaps?
Does the memory allocated need to be contiguous?
 
Old 10-01-2010, 04:32 AM   #4
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
Not the memory which will be allocated need not be contiguous.
It can be like one byte at some location another byte at some other location and if a user space system call has requested
say k bytes then my program should be able to find these k bytes which may not be at same place but a group of blocks on 1 byte or 2 or 3 or more and a combination of these whose sum total is k bytes.

What advantage will I get using array out of matrix.
Imagine a real situation you want to write some data in RAM then would you consider the different memory locations on RAM as array or you would choose it as a matrix.
 
Old 10-01-2010, 05:28 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The quick answer is a linked list.

The more detailed answer along with caveats may come after I've completed my parental duties
 
Old 10-01-2010, 08:52 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Okay here is one approach:

You can build a linked list that in the simplest case has a node that contains a reference to the first part of memory (the address) and the amount of memory allocated and the pointer to the next node in the list allowing for non-contiguous blocks of memory.
Then you can have a second list that consists of these linked lists as nodes, along with details of the process that has allocated the memory, one of these will be the free memory list, allowing you to quickly locate unallocated memory, and to free previously allocated memory.

If you are allocating a lot of small blocks of memory the overhead of this approach is going to be quite significant otherwise it shouldn't be too much of a problem.
 
Old 10-02-2010, 04:39 AM   #7
jamesbon
Member
 
Registered: Jun 2010
Posts: 147

Original Poster
Rep: Reputation: 9
Ok some one suggested me to be able to use heaps as glibc has implemented.
If you understand what this suggestion means can you give me some link in glibc where this has been actually done.
 
  


Reply



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
How can i manage data on my server? thep General 2 08-12-2007 11:39 PM
data structure for holding a Table in memory xface66 Programming 4 04-10-2007 06:30 PM
Data structure trie spank Programming 7 05-21-2006 07:21 AM
Linux Memory Structure ashley75 Linux - General 5 06-07-2005 02:25 PM
Manage Data Base in c/c++ saint_devil Programming 3 01-03-2005 09:33 AM

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

All times are GMT -5. The time now is 07:49 PM.

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