Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 0.82 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Void * space in a struct instead of malloc
  2. typedef struct Entry {
  3.     int counter;
  4.     void *block;
  5. } Entry;
  6.        
  7. void *memPtr = mmap(NULL, someSize*1024, PROT_READ|PROT_WRITE,
  8.                         MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  9.        
  10. int AddEntry(void *data) {
  11.    Entry entry;
  12.    entry.counter = 1;
  13.    entry.block = malloc(sizeof(char *) * SECTOR_SIZE);
  14.    memcpy(entry.block, data, SECTOR_SIZE);
  15.    memcpy(&memPtr[0], &entry, sizeof(Entry));
  16.    return 0;
  17. }
  18.        
  19. int AddEntry(void *data) {
  20.    Entry *entry;
  21.    entry = malloc(sizeof(Entry) + ((SECTOR_SIZE-1) * sizeof(unsigned char));
  22.    // obligitory NULL checks assumed here
  23.  
  24. // fill in structure
  25.    entry->counter = 1;
  26.    memcpy(entry->block, data, SECTOR_SIZE);
  27.    memcpy(&memPtr[0], &entry, sizeof(Entry) + ((SECTOR_SIZE-1) * sizeof(unsigned char));
  28.    return 0;
  29. }
  30.        
  31. block