Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ALLOCSIZE 100
- static char allocbuf[ALLOCSIZE];
- static char *allocp = &allocbuf[0];
- static void movemem(char *start);
- char *smartalloc(int n, char *begin);
- /**
- * n - lenght of asking memory
- * *begin - pointer to beginning interest blocks
- */
- char *smartalloc(int n, char *begin)
- {
- /* if first calling, begin set to buffer-begin */
- begin = (begin == 0) ? allocbuf : begin; // begin == 0 eq begin == NULL
- if (allocbuf + ALLOCSIZE - allocp >= n) {
- allocp += n;
- return allocp - n;
- }
- else { /* buffer full */
- movemem(begin);
- if (allocbuf + ALLOCSIZE - allocp >= n) {
- allocp += n;
- return allocp - n;
- }
- else
- return 0; /* movemem does not solve problem */
- }
- }
- /**
- * move important memory blocks (it start from *start) to buffer-begin
- */
- static void movemem(char *begin)
- {
- if (begin > allocbuf && begin < allocp) {
- char *p = &allocbuf[0];
- while (begin < allocp)
- *p++ = *begin++;
- allocp = p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement