473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C - User Created Heap Testing

he main routine runs a minimum test

Here is the code
http://phpfi.com/332069


here is the assignment
http://phpfi.com/332068

I would appreciate if someone can review/approve this code
Jul 14 '08 #1
3 1486
JosAH
11,448 Expert 8TB
The links are dead; better post your code etc. here.

kind regards,

Jos
Jul 14 '08 #2
Strangely, the hyperlink does not work I don't know why, you can still copy paste the links.

Anyway, here is the assignment:

Memory: User created Heap
write a program to have an array of 2mb character storage. segment the memory
into a linked list as per following rules:

1.each node in the list has a header and payload.

2.header should have 2 members
i.pointer to the next node
ii.allocation status of the current node

3.payload should be 16byte chunk of memory .all ur applications dynamic
memory requirement where buffer size is less than or equal to 16 should not use
malloc ,memory should be allocated from local linked lists

4.user should allocate/free the memory in any order

5.design two functions to allocate/free memory names of functions should
be getbuf and freebuf
fn prototypes are
void *getbuf(size_t size);
void freebuf(void* buf);

6.if all ur 16byte buffers int the list are exhausted or if the size of memory
asked by user is more than 16bytes call malloc from within getbuf()



And here is the code that I have done:



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. #define NDEBUG
  6. /* 2MB memory all filled with 0 */
  7. #define KB (1024)
  8. #define MB (KB*KB)
  9.  
  10. #define MEM_SIZE (2*MB)
  11. char memory[MEM_SIZE];
  12. #define LIMIT ((void *)(memory + MEM_SIZE - 1))
  13.  
  14. #define MAX_CHUNK 16 //The maximum chunk of memory we will return else we prefer malloc
  15.  
  16.  
  17. #define NODE_ALLOCATED 1
  18. #define NODE_FREE      0
  19.  
  20. struct memory_node
  21. {
  22.     struct memory_node *next;
  23.     int    status;
  24.     size_t sz;
  25.     void *payload;
  26. };
  27. struct memory_node *memlist = (struct memory_node *)memory;
  28.  
  29. static inline
  30. int check_limit_get(struct memory_node *ptr,size_t sz)
  31. {
  32.     if (((void*)ptr + sizeof(struct memory_node) + sz) > LIMIT)
  33.          return -1;
  34.     return 0;
  35. }
  36.  
  37. static inline
  38. int check_limit_free(void *ptr)
  39. {
  40.     if (ptr > LIMIT)
  41.          return -1;
  42.     return 0;
  43. }
  44.  
  45.  
  46. static
  47. void* work_getbuf(size_t sz)
  48. {
  49.     struct memory_node *mem_node;
  50.     struct memory_node *next;
  51.     assert (sz <= MAX_CHUNK);
  52.     printf("In work_getbuf sz = %d\n",sz);
  53.     /* The semicolon at the end of the loop is intentional
  54.        We will loop till we find a node that has size that
  55.        can accomodate the required size
  56.     */
  57.     for (mem_node = memlist;
  58.         (mem_node->status == NODE_ALLOCATED) || (mem_node->sz < sz);
  59.          mem_node = mem_node->next)
  60.     ;
  61.     printf("Crossed for loop \n");
  62.     /* We reached here means the current mem_node is free and
  63.        it has enough space that has been requested
  64.     */
  65.     assert ((mem_node->status == NODE_FREE) && (mem_node->sz >=sz));
  66.     if (check_limit_get(mem_node,sz) < 0)
  67.          return malloc(sz);  // Lets not spoil someone's party if we have don't have space
  68.     mem_node->status = NODE_ALLOCATED;
  69.     if (mem_node->sz > (sz + sizeof(struct memory_node)))
  70.     {
  71.  
  72.         /* We come here if we have some space to squeeze */
  73.         next = mem_node->next;
  74.         mem_node->next =
  75.                 (struct memory_node*)((void*)mem_node + mem_node->sz + sizeof(struct memory_node));
  76.         mem_node->next->sz = mem_node->sz - (sz + sizeof(struct memory_node));
  77.         mem_node->next->next = next;
  78.         mem_node->sz = sz;
  79.     }
  80.     mem_node->payload = (void *)mem_node + sizeof(struct memory_node);
  81.     return mem_node->payload;
  82. }
  83.  
  84. static inline
  85. void* getbuf(size_t sz)
  86. {
  87.     if (sz > MAX_CHUNK)
  88.         return malloc(sz);
  89.     return work_getbuf(sz);
  90. }
  91.  
  92. static inline
  93. void freebuf(void *ptr)
  94. {
  95.     struct memory_node *mem_node;
  96.     if (check_limit_free(ptr) < 0)
  97.     {
  98.         /* The pointer does not belong to us, we pass the trouble to ANSI free :) */
  99.         free(ptr);
  100.         return;
  101.     }
  102.     mem_node = ptr - sizeof(struct memory_node);
  103.     mem_node->status = NODE_FREE;
  104. }
  105.  
  106. void verify_begin(void)
  107. {
  108.     assert(MAX_CHUNK <= MEM_SIZE);
  109.     memlist->status = NODE_FREE;
  110.     memlist->sz     = MEM_SIZE - sizeof(struct memory_node);
  111.     memlist->next   = NULL;
  112. }
  113.  
  114. int main()
  115. {
  116.     char *char_ptrs[100];
  117.     int i;
  118.     verify_begin();
  119.     for (i = 0; i < 100; i++)
  120.     {
  121.         char_ptrs[i] = getbuf(i%25); //Some blocks greater than 16 and some less
  122.     }
  123.     for (i = 99; i>=0; i--)
  124.     {
  125.         freebuf(char_ptrs[i]);
  126.     }
  127.     return 0;
  128.  
  129. }
Jul 14 '08 #3
I could fix some bugs. It is running the test. I am not sure if someone is up for it but at least I get motivation to test more by posting here.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. #define NDEBUG
  6. /* 2MB memory all filled with 0 */
  7. #define KB (1024)
  8. #define MB (KB*KB)
  9.  
  10. #define MEM_SIZE (2*MB)
  11. char memory[MEM_SIZE];
  12. #define LIMIT ((void *)(memory + MEM_SIZE - 1))
  13.  
  14. #define MAX_CHUNK 16 //The maximum chunk of memory we will return else we prefer malloc
  15.  
  16.  
  17. #define NODE_ALLOCATED 1
  18. #define NODE_FREE      0
  19.  
  20. struct memory_node
  21. {
  22.     struct memory_node *next;
  23.     int    status;
  24.     size_t sz;
  25.     void *payload;
  26. };
  27. struct memory_node *memlist = (struct memory_node *)memory;
  28.  
  29. static inline
  30. int check_limit_get(struct memory_node *ptr,size_t sz)
  31. {
  32.     if (((void*)ptr + sizeof(struct memory_node) + sz) > LIMIT)
  33.          return -1;
  34.     return 0;
  35. }
  36.  
  37. static inline
  38. int check_limit_free(void *ptr)
  39. {
  40.     if ((ptr > LIMIT)||(ptr<(void *)memory))
  41.          return -1;
  42.     return 0;
  43. }
  44.  
  45.  
  46. static
  47. void* work_getbuf(size_t sz)
  48. {
  49.     struct memory_node *mem_node;
  50.     struct memory_node *next;
  51.     assert (sz <= MAX_CHUNK &&sz != 0);
  52.     //printf("In work_getbuf sz = %d\n",sz);
  53.     /* The semicolon at the end of the loop is intentional
  54.        We will loop till we find a node that has size that
  55.        can accomodate the required size
  56.     */
  57.     for (mem_node = memlist;
  58.         (mem_node->status == NODE_ALLOCATED) || (mem_node->sz < sz);
  59.          mem_node = mem_node->next)
  60.     {
  61.  
  62.     }
  63.  
  64.     //printf("Crossed for loop \n");
  65.     /* We reached here means the current mem_node is free and
  66.        it has enough space that has been requested
  67.     */
  68.     assert ((mem_node->status == NODE_FREE) && (mem_node->sz >=sz));
  69.     if (check_limit_get(mem_node,sz) < 0)
  70.          return malloc(sz);  // Lets not spoil someone's party if we have don't have space
  71.     mem_node->status = NODE_ALLOCATED;
  72.     if (mem_node->sz > (sz + sizeof(struct memory_node)))
  73.     {
  74.  
  75.         /* We come here if we have some space to squeeze */
  76.         next = mem_node->next;
  77.         mem_node->next =
  78.                 (struct memory_node*)
  79.                       ((void*)mem_node + sz + sizeof(struct memory_node));
  80.         mem_node->next->sz = mem_node->sz - (sz + sizeof(struct memory_node));
  81.         mem_node->next->next = next;
  82.         mem_node->next->status = NODE_FREE;
  83.         mem_node->sz = sz;
  84.     }
  85.     mem_node->payload = (void *)mem_node + sizeof(struct memory_node);
  86.     return mem_node->payload;
  87. }
  88.  
  89. static inline
  90. void* getbuf(size_t sz)
  91. {
  92.     if (sz > MAX_CHUNK)
  93.         return malloc(sz);
  94.     return work_getbuf(sz);
  95. }
  96.  
  97. static inline
  98. void freebuf(void *ptr)
  99. {
  100.     struct memory_node *mem_node;
  101.     if (check_limit_free(ptr) < 0)
  102.     {
  103.         /* The pointer does not belong to us, we pass the trouble to ANSI free :) */
  104.         free(ptr);
  105.         return;
  106.     }
  107.     //printf("In free buffer\n");
  108.     mem_node = ptr - sizeof(struct memory_node);
  109.     assert(mem_node->status == NODE_ALLOCATED);
  110.     mem_node->status = NODE_FREE;
  111. }
  112.  
  113. void verify_begin(void)
  114. {
  115.     assert(MAX_CHUNK <= MEM_SIZE);
  116.     memlist->status = NODE_FREE;
  117.     memlist->sz     = MEM_SIZE - sizeof(struct memory_node);
  118.     memlist->next   = NULL;
  119. }
  120.  
  121. void memlist_walk()
  122. {
  123.     int i;
  124.     struct memory_node *mem_node;
  125.     //printf("In memlist walk\n");
  126.     for (i = 1,mem_node = memlist;mem_node != NULL; i++,mem_node=mem_node->next)
  127.     {
  128.         printf("%d.",i);
  129.         printf("status = %d", mem_node->status);
  130.         printf("    sz = %d",mem_node->sz);
  131.         printf("\n");
  132.     }
  133.  
  134. }
  135. int main()
  136. {
  137.     char *char_ptrs[100];
  138.     int i=0;
  139.     size_t sz;
  140.     verify_begin();
  141.     for (i = 0; i < 100; i++)
  142.     {
  143.         sz = i%25;
  144.         if (sz == 0)
  145.         {
  146.             sz = 100;
  147.         }
  148.         char_ptrs[i] = getbuf(sz); //Some blocks greater than 16 and some less
  149.         //memlist_walk();
  150.     }
  151.     for (i = 99; i >= 0; i--)
  152.     {
  153.         freebuf(char_ptrs[i]);
  154.     }
  155.     return 0;
  156. }
Jul 15 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: opistobranchia | last post by:
blah F test = getF(); // Print out shows that an F object was destroyed by the ~F. F set var to 0 on delete; test.print(); //reveals that var is still 123 value is 123 still instead of 0 F...
3
by: Joerg Toellner | last post by:
Dear Group, i have a self written c++-class that is used in many projects from many programmers. The functionality of this class requests, for some reasons, that any instance (object) of this...
7
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
6
by: Victor Nazarov | last post by:
So seems to be slightly wrong group, but... The question is: Is there any extentions for C or well known libraries to implement heap managment, for example if I want to allocate a randome data...
12
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
15
by: Jason | last post by:
Currently, I am storing information about the currently logged on user in Session variables that are stored in SQL. However, I am using role-based security, so I am storing custom roles in a...
4
by: Justin | last post by:
Recieved the message: SQL0954C Not enough storage is available in the application heap to process the statement. SQLSTATE=57011 After viewing other posts, they recommended changing the...
1
by: =?Utf-8?B?TmFyYXlhbmFu?= | last post by:
Are there any way to prove that a variable of value type is created in stack and a variable of ref type are created in heap? I just take it granted as everybody and every books and pages says so....
13
by: aarklon | last post by:
Hi all, Is there a possible way to check the status of heap in ANSI-C or standard C....???? if my memory is correct in the old turbo c++ 3.0 compiler there were some macros by name _HEAPOK,...
23
by: andyoye | last post by:
How can I launch Outlook on users machines when they click a button on a web form (InfoPath)? Thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.