rmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. typedef struct Map Map;
  15. struct Map {
  16. uint32_t size;
  17. uint32_t addr;
  18. };
  19. typedef struct RMap RMap;
  20. struct RMap {
  21. char* name;
  22. Map* map;
  23. Map* mapend;
  24. Lock Lock;
  25. };
  26. void
  27. rmapfree(RMap* rmap, uintptr_t addr, uint size)
  28. {
  29. Map *mp;
  30. uint t;
  31. if(size == 0)
  32. return;
  33. lock(&rmap->Lock);
  34. for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
  35. ;
  36. if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
  37. (mp-1)->size += size;
  38. if(addr+size == mp->addr){
  39. (mp-1)->size += mp->size;
  40. while(mp->size){
  41. mp++;
  42. (mp-1)->addr = mp->addr;
  43. (mp-1)->size = mp->size;
  44. }
  45. }
  46. }else{
  47. if(addr+size == mp->addr && mp->size){
  48. mp->addr -= size;
  49. mp->size += size;
  50. }else{
  51. do{
  52. if(mp >= rmap->mapend){
  53. print("mapfree: %s: losing 0x%lX, %u\n", rmap->name, addr, size);
  54. break;
  55. }
  56. t = mp->addr;
  57. mp->addr = addr;
  58. addr = t;
  59. t = mp->size;
  60. mp->size = size;
  61. mp++;
  62. }while((size = t) != 0);
  63. }
  64. }
  65. unlock(&rmap->Lock);
  66. }
  67. uintptr_t
  68. rmapalloc(RMap* rmap, uintptr_t addr, uint size, int align)
  69. {
  70. Map *mp;
  71. uint32_t maddr, oaddr;
  72. lock(&rmap->Lock);
  73. for(mp = rmap->map; mp->size; mp++){
  74. maddr = mp->addr;
  75. if(addr){
  76. /*
  77. * A specific address range has been given:
  78. * if the current map entry is greater then
  79. * the address is not in the map;
  80. * if the current map entry does not overlap
  81. * the beginning of the requested range then
  82. * continue on to the next map entry;
  83. * if the current map entry does not entirely
  84. * contain the requested range then the range
  85. * is not in the map.
  86. */
  87. if(maddr > addr)
  88. break;
  89. if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
  90. continue;
  91. if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
  92. break;
  93. maddr = addr;
  94. }
  95. if(align > 0)
  96. maddr = ((maddr+align-1)/align)*align;
  97. if(mp->addr+mp->size-maddr < size)
  98. continue;
  99. oaddr = mp->addr;
  100. mp->addr = maddr+size;
  101. mp->size -= maddr-oaddr+size;
  102. if(mp->size == 0){
  103. do{
  104. mp++;
  105. (mp-1)->addr = mp->addr;
  106. }while((mp-1)->size = mp->size);
  107. }
  108. unlock(&rmap->Lock);
  109. if(oaddr != maddr)
  110. rmapfree(rmap, oaddr, maddr-oaddr);
  111. return maddr;
  112. }
  113. unlock(&rmap->Lock);
  114. return 0;
  115. }