rmap.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. void
  15. rmapfree(RMap* rmap, uintptr addr, uint size)
  16. {
  17. Map *mp;
  18. uint t;
  19. if(size == 0)
  20. return;
  21. lock(rmap);
  22. for(mp = rmap->map; mp->addr <= addr && mp->size; mp++)
  23. ;
  24. if(mp > rmap->map && (mp-1)->addr+(mp-1)->size == addr){
  25. (mp-1)->size += size;
  26. if(addr+size == mp->addr){
  27. (mp-1)->size += mp->size;
  28. while(mp->size){
  29. mp++;
  30. (mp-1)->addr = mp->addr;
  31. (mp-1)->size = mp->size;
  32. }
  33. }
  34. }else{
  35. if(addr+size == mp->addr && mp->size){
  36. mp->addr -= size;
  37. mp->size += size;
  38. }else{
  39. do{
  40. if(mp >= rmap->mapend){
  41. print("mapfree: %s: losing 0x%luX, %ud\n", rmap->name, addr, size);
  42. break;
  43. }
  44. t = mp->addr;
  45. mp->addr = addr;
  46. addr = t;
  47. t = mp->size;
  48. mp->size = size;
  49. mp++;
  50. }while((size = t) != 0);
  51. }
  52. }
  53. unlock(rmap);
  54. }
  55. uintptr
  56. rmapalloc(RMap* rmap, uintptr addr, uint size, int align)
  57. {
  58. Map *mp;
  59. uint32_t maddr, oaddr;
  60. lock(rmap);
  61. for(mp = rmap->map; mp->size; mp++){
  62. maddr = mp->addr;
  63. if(addr){
  64. /*
  65. * A specific address range has been given:
  66. * if the current map entry is greater then
  67. * the address is not in the map;
  68. * if the current map entry does not overlap
  69. * the beginning of the requested range then
  70. * continue on to the next map entry;
  71. * if the current map entry does not entirely
  72. * contain the requested range then the range
  73. * is not in the map.
  74. */
  75. if(maddr > addr)
  76. break;
  77. if(mp->size < addr - maddr) /* maddr+mp->size < addr, but no overflow */
  78. continue;
  79. if(addr - maddr > mp->size - size) /* addr+size > maddr+mp->size, but no overflow */
  80. break;
  81. maddr = addr;
  82. }
  83. if(align > 0)
  84. maddr = ((maddr+align-1)/align)*align;
  85. if(mp->addr+mp->size-maddr < size)
  86. continue;
  87. oaddr = mp->addr;
  88. mp->addr = maddr+size;
  89. mp->size -= maddr-oaddr+size;
  90. if(mp->size == 0){
  91. do{
  92. mp++;
  93. (mp-1)->addr = mp->addr;
  94. }while((mp-1)->size = mp->size);
  95. }
  96. unlock(rmap);
  97. if(oaddr != maddr)
  98. rmapfree(rmap, oaddr, maddr-oaddr);
  99. return maddr;
  100. }
  101. unlock(rmap);
  102. return 0;
  103. }