Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/runtime/Kernel/xmem.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "xmem.h"


typedef struct MEMORYBLOCK {
  struct MEMORYBLOCK *next;
  char   memory[BLOCKSIZE];
} MemoryBlock;

int MemoryPosition;


MemoryBlock *EmptyBlock;
MemoryBlock *WorkingBlock;
MemoryBlock *FullBlock;
int MemoryPosition = BLOCKSIZE; /* Start with no block */

void *xmalloc(int size)
{
  void *new;
  if(MemoryPosition+size > BLOCKSIZE) {
    if(!EmptyBlock) {
      if(0==(EmptyBlock = (MemoryBlock *)malloc(sizeof(MemoryBlock)))) {
        fprintf(stderr,"Out of memory in xmalloc in profile.c\n");
        exit(-1);
      } else {
        EmptyBlock->next = 0;
      }
    }
    WorkingBlock = EmptyBlock;
    EmptyBlock = EmptyBlock->next;
    MemoryPosition = 0;
  }
  new = &WorkingBlock->memory[MemoryPosition];
  MemoryPosition += size;
  return new;
}

void xfree(void)
{
  MemoryBlock *mb;

  if(0 != (mb = FullBlock)) {
    while(mb->next)
      mb = mb->next;
    mb->next = EmptyBlock;
    EmptyBlock = FullBlock;
    FullBlock = 0;
  }
  if(WorkingBlock) {
    WorkingBlock->next = EmptyBlock;
    EmptyBlock = WorkingBlock;
    WorkingBlock = 0;
  }
  MemoryPosition = BLOCKSIZE;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].