ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.