PageRenderTime 68ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/kernel/blockheap.c

https://github.com/sergei/minigui
C | 129 lines | 90 code | 25 blank | 14 comment | 14 complexity | 9ba6dbdb6946eac97a2f6b3904855a57 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0
  1. /*
  2. ** $Id: blockheap.c 7339 2007-08-16 03:47:29Z xgwang $
  3. **
  4. ** blockheap.c: The heap of block data.
  5. **
  6. ** Copyright (C) 2003 ~ 2007 Feynman Software.
  7. ** Copyright (C) 1999 ~ 2002 Wei Yongming.
  8. **
  9. ** All rights reserved by Feynman Software.
  10. **
  11. ** Current maintainer: Wei Yongming.
  12. **
  13. ** Create date: 2001/01/10
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include "common.h"
  20. #include "minigui.h"
  21. #include "gdi.h"
  22. #include "blockheap.h"
  23. void InitBlockDataHeap (PBLOCKHEAP heap, size_t bd_size, size_t heap_size)
  24. {
  25. #ifndef _LITE_VERSION
  26. pthread_mutex_init (&heap->lock, NULL);
  27. #endif
  28. heap->heap = NULL;
  29. heap->bd_size = bd_size + sizeof (DWORD);
  30. heap->heap_size = heap_size;
  31. }
  32. void* BlockDataAlloc (PBLOCKHEAP heap)
  33. {
  34. int i;
  35. char* block_data = NULL;
  36. #ifndef _LITE_VERSION
  37. pthread_mutex_lock (&heap->lock);
  38. #endif
  39. if (heap->heap == NULL) {
  40. if (!(heap->heap = calloc (heap->heap_size, heap->bd_size)))
  41. goto ret;
  42. heap->free = 0;
  43. }
  44. block_data = (char*) heap->heap + heap->bd_size*heap->free;
  45. for (i = heap->free; i < heap->heap_size; i++) {
  46. if (*((DWORD*)block_data) == BDS_FREE) {
  47. heap->free = i + 1;
  48. *((DWORD*)block_data) = BDS_USED;
  49. #if 0
  50. fprintf (stderr, "heap: %p, Allocated: %d, free: %d.\n", heap, i, heap->free);
  51. fprintf (stderr, "Heap: (bd_size: %d, heap_size: %d, heap: %p).\n",
  52. heap->bd_size, heap->heap_size, heap->heap);
  53. #endif
  54. goto ret;
  55. }
  56. block_data += heap->bd_size;
  57. }
  58. if (!(block_data = calloc (1, heap->bd_size)))
  59. goto ret;
  60. *((DWORD*)block_data) = BDS_SPECIAL;
  61. ret:
  62. #ifndef _LITE_VERSION
  63. pthread_mutex_unlock (&heap->lock);
  64. #endif
  65. if (block_data)
  66. return block_data + sizeof (DWORD);
  67. return NULL;
  68. }
  69. void BlockDataFree (PBLOCKHEAP heap, void* data)
  70. {
  71. int i;
  72. char* block_data;
  73. #ifndef _LITE_VERSION
  74. pthread_mutex_lock (&heap->lock);
  75. #endif
  76. block_data = (char*) data - sizeof (DWORD);
  77. if (*((DWORD*)block_data) == BDS_SPECIAL)
  78. free (block_data);
  79. else if (*((DWORD*)block_data) == BDS_USED) {
  80. *((DWORD*)block_data) = BDS_FREE;
  81. i = (block_data - (char*)heap->heap)/heap->bd_size;
  82. if (heap->free > i)
  83. heap->free = i;
  84. #if 0
  85. fprintf (stderr, "Heap: %p: Freed: %d, free: %d.\n", heap, i, heap->free);
  86. fprintf (stderr, "Heap: (bd_size: %d, heap_size: %d, heap: %p).\n",
  87. heap->bd_size, heap->heap_size, heap->heap);
  88. #endif
  89. }
  90. #ifndef _LITE_VERSION
  91. pthread_mutex_unlock (&heap->lock);
  92. #endif
  93. }
  94. void DestroyBlockDataHeap (PBLOCKHEAP heap)
  95. {
  96. #if 0
  97. fprintf (stderr, "Heap: (bd_size: %d, heap_size: %d, heap: %p).\n",
  98. heap->bd_size, heap->heap_size, heap->heap);
  99. #endif
  100. #ifndef _LITE_VERSION
  101. pthread_mutex_destroy (&heap->lock);
  102. #endif
  103. free (heap->heap);
  104. }