Advertisement
Guest User

alloc.c

a guest
Jan 17th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #define ALLOCSIZE 100
  2.  
  3. static char allocbuf[ALLOCSIZE];
  4. static char *allocp = &allocbuf[0];
  5.  
  6. static void movemem(char *start);
  7. char *smartalloc(int n, char *begin);
  8.    
  9. /**
  10.  * n - lenght of asking memory
  11.  * *begin - pointer to beginning interest blocks
  12.  */
  13. char *smartalloc(int n, char *begin)
  14. {
  15.     /* if first calling, begin set to buffer-begin */
  16.     begin = (begin == 0) ? allocbuf : begin; // begin == 0 eq begin == NULL
  17.     if (allocbuf + ALLOCSIZE - allocp >= n) {
  18.         allocp += n;
  19.         return allocp - n;
  20.     }
  21.     else {  /* buffer full */
  22.         movemem(begin);
  23.         if (allocbuf + ALLOCSIZE - allocp >= n) {
  24.             allocp += n;
  25.             return allocp - n;
  26.         }
  27.         else
  28.             return 0; /* movemem does not solve problem */
  29.     }
  30. }
  31.  
  32. /**
  33.  * move important memory blocks (it start from *start) to buffer-begin
  34.  */
  35. static void movemem(char *begin)
  36. {
  37.     if (begin > allocbuf && begin < allocp) {
  38.         char *p = &allocbuf[0];
  39.         while (begin < allocp)
  40.             *p++ = *begin++;
  41.         allocp = p;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement