shm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_general.h"
  17. #include "fspr_shm.h"
  18. #include "fspr_errno.h"
  19. #include "fspr_lib.h"
  20. #include "fspr_strings.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <kernel/OS.h>
  24. #include "fspr_portable.h"
  25. struct fspr_shm_t {
  26. fspr_pool_t *pool;
  27. void *memblock;
  28. void *ptr;
  29. fspr_size_t reqsize;
  30. fspr_size_t avail;
  31. area_id aid;
  32. };
  33. APR_DECLARE(fspr_status_t) fspr_shm_create(fspr_shm_t **m,
  34. fspr_size_t reqsize,
  35. const char *filename,
  36. fspr_pool_t *p)
  37. {
  38. fspr_size_t pagesize;
  39. area_id newid;
  40. char *addr;
  41. char shname[B_OS_NAME_LENGTH];
  42. (*m) = (fspr_shm_t *)fspr_pcalloc(p, sizeof(fspr_shm_t));
  43. /* we MUST allocate in pages, so calculate how big an area we need... */
  44. pagesize = ((reqsize + B_PAGE_SIZE - 1) / B_PAGE_SIZE) * B_PAGE_SIZE;
  45. if (!filename) {
  46. int num = 0;
  47. snprintf(shname, B_OS_NAME_LENGTH, "fspr_shmem_%ld", find_thread(NULL));
  48. while (find_area(shname) >= 0)
  49. snprintf(shname, B_OS_NAME_LENGTH, "fspr_shmem_%ld_%d",
  50. find_thread(NULL), num++);
  51. }
  52. newid = create_area(filename ? filename : shname,
  53. (void*)&addr, B_ANY_ADDRESS,
  54. pagesize, B_LAZY_LOCK, B_READ_AREA|B_WRITE_AREA);
  55. if (newid < 0)
  56. return errno;
  57. (*m)->pool = p;
  58. (*m)->aid = newid;
  59. (*m)->memblock = addr;
  60. (*m)->ptr = (void*)addr;
  61. (*m)->avail = pagesize; /* record how big an area we actually created... */
  62. (*m)->reqsize = reqsize;
  63. return APR_SUCCESS;
  64. }
  65. APR_DECLARE(fspr_status_t) fspr_shm_destroy(fspr_shm_t *m)
  66. {
  67. delete_area(m->aid);
  68. m->avail = 0;
  69. m->memblock = NULL;
  70. return APR_SUCCESS;
  71. }
  72. APR_DECLARE(fspr_status_t) fspr_shm_remove(const char *filename,
  73. fspr_pool_t *pool)
  74. {
  75. area_id deleteme = find_area(filename);
  76. if (deleteme == B_NAME_NOT_FOUND)
  77. return APR_EINVAL;
  78. delete_area(deleteme);
  79. return APR_SUCCESS;
  80. }
  81. APR_DECLARE(fspr_status_t) fspr_shm_attach(fspr_shm_t **m,
  82. const char *filename,
  83. fspr_pool_t *pool)
  84. {
  85. area_info ai;
  86. thread_info ti;
  87. fspr_shm_t *new_m;
  88. area_id deleteme = find_area(filename);
  89. if (deleteme == B_NAME_NOT_FOUND)
  90. return APR_EINVAL;
  91. new_m = (fspr_shm_t*)fspr_palloc(pool, sizeof(fspr_shm_t*));
  92. if (new_m == NULL)
  93. return APR_ENOMEM;
  94. new_m->pool = pool;
  95. get_area_info(deleteme, &ai);
  96. get_thread_info(find_thread(NULL), &ti);
  97. if (ti.team != ai.team) {
  98. area_id narea;
  99. narea = clone_area(ai.name, &(ai.address), B_CLONE_ADDRESS,
  100. B_READ_AREA|B_WRITE_AREA, ai.area);
  101. if (narea < B_OK)
  102. return narea;
  103. get_area_info(narea, &ai);
  104. new_m->aid = narea;
  105. new_m->memblock = ai.address;
  106. new_m->ptr = (void*)ai.address;
  107. new_m->avail = ai.size;
  108. new_m->reqsize = ai.size;
  109. }
  110. (*m) = new_m;
  111. return APR_SUCCESS;
  112. }
  113. APR_DECLARE(fspr_status_t) fspr_shm_detach(fspr_shm_t *m)
  114. {
  115. delete_area(m->aid);
  116. return APR_SUCCESS;
  117. }
  118. APR_DECLARE(void *) fspr_shm_baseaddr_get(const fspr_shm_t *m)
  119. {
  120. return m->memblock;
  121. }
  122. APR_DECLARE(fspr_size_t) fspr_shm_size_get(const fspr_shm_t *m)
  123. {
  124. return m->reqsize;
  125. }
  126. APR_POOL_IMPLEMENT_ACCESSOR(shm)
  127. APR_DECLARE(fspr_status_t) fspr_os_shm_get(fspr_os_shm_t *osshm,
  128. fspr_shm_t *shm)
  129. {
  130. return APR_ENOTIMPL;
  131. }
  132. APR_DECLARE(fspr_status_t) fspr_os_shm_put(fspr_shm_t **m,
  133. fspr_os_shm_t *osshm,
  134. fspr_pool_t *pool)
  135. {
  136. return APR_ENOTIMPL;
  137. }