Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. /* MZM - memory mapped disk baked toy allocator.
  2. *
  3. * Copyright 2010 Salvatore Sanfilippo, BSD licensed. */
  4.  
  5. #include <sys/mman.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11.  
  12. #define MEMORYZM_INITIAL_SIZE (4096*1024*8)
  13. #define MZM_BASE_ADDR 0x10000000
  14.  
  15. static unsigned long cursize, curoff;
  16.  
  17. /* Returns NULL if there was no pre-existing state.
  18. * Otherwise the address of the first allocation is returned. */
  19. void *zmzInit(void) {
  20. int fd = open("memory.zm",O_CREAT|O_RDWR,0644);
  21. struct stat buf;
  22. /* The first allocation is at base address plus a first long value
  23. * used to store the current offset, plus a second long value that
  24. * is the header of every allocation. */
  25. void *retval = (void*)(MZM_BASE_ADDR+(sizeof(long)*2));
  26.  
  27. if (fd == -1) {
  28. perror("opening memory.zm");
  29. exit(1);
  30. }
  31. if (fstat(fd, &buf) == -1) {
  32. perror("fstat(2) against memory.zm");
  33. exit(1);
  34. }
  35.  
  36. /* Mmap the file starting from our base address */
  37. cursize = buf.st_size;
  38.  
  39. /* If there was not pre-existing state initalize it, otherwise
  40. * just read the old offset. */
  41. printf("cursize: %lu\n",cursize);
  42. if (cursize == 0) {
  43. long *x;
  44.  
  45. retval = NULL;
  46. if (ftruncate(fd,MEMORYZM_INITIAL_SIZE) == -1) {
  47. perror("ftruncate(2) against memory.zm");
  48. exit(1);
  49. }
  50. cursize = MEMORYZM_INITIAL_SIZE;
  51. printf("Mmap-ping...\n");
  52. if (mmap((void*)MZM_BASE_ADDR,cursize,PROT_READ|PROT_WRITE,
  53. MAP_FILE|MAP_FIXED|MAP_SHARED,fd,0) == MAP_FAILED) {
  54. perror("mmap(2) in MZM");
  55. exit(1);
  56. }
  57. printf("OK\n"); fflush(stdout);
  58.  
  59. curoff = (unsigned long) (MZM_BASE_ADDR+sizeof(long));
  60. x = (long*) MZM_BASE_ADDR;
  61. *x = curoff;
  62. } else {
  63. long *x;
  64.  
  65. if (mmap((void*)MZM_BASE_ADDR,cursize,PROT_READ|PROT_WRITE,
  66. MAP_FILE|MAP_FIXED|MAP_SHARED,fd,0) == MAP_FAILED) {
  67. perror("mmap(2) in MZM");
  68. exit(1);
  69. }
  70.  
  71. x = (long*) MZM_BASE_ADDR;
  72. curoff = *x;
  73. }
  74. return retval;
  75. }
  76.  
  77. void *zmalloc(size_t size) {
  78. unsigned long *x = (unsigned long*) curoff;
  79. unsigned long *curoffp = (unsigned long*) MZM_BASE_ADDR;
  80.  
  81. curoff += sizeof(unsigned long)+size;
  82. *curoffp = curoff;
  83. *x = (unsigned long) size;
  84. x++;
  85. return x;
  86. }
  87.  
  88. #include <string.h>
  89. int main(void) {
  90. void *firstalloc = zmzInit();
  91. char *p;
  92.  
  93. if (firstalloc == NULL) {
  94. printf("First allocation\n");
  95. p = zmalloc(7);
  96. memcpy(p,"foobar\0",7);
  97. } else {
  98. printf("Second run\n");
  99. p = firstalloc;
  100. }
  101. printf("String is: %s\n", p);
  102. return 0;
  103. }
Add Comment
Please, Sign In to add comment