shm.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "fspr_portable.h"
  22. struct fspr_shm_t {
  23. fspr_pool_t *pool;
  24. void *memblock;
  25. };
  26. APR_DECLARE(fspr_status_t) fspr_shm_create(fspr_shm_t **m,
  27. fspr_size_t reqsize,
  28. const char *filename,
  29. fspr_pool_t *pool)
  30. {
  31. int rc;
  32. fspr_shm_t *newm = (fspr_shm_t *)fspr_palloc(pool, sizeof(fspr_shm_t));
  33. char *name = NULL;
  34. ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
  35. newm->pool = pool;
  36. if (filename) {
  37. name = fspr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
  38. }
  39. if (name == NULL) {
  40. flags |= OBJ_GETTABLE;
  41. }
  42. rc = DosAllocSharedMem(&(newm->memblock), name, reqsize, flags);
  43. if (rc) {
  44. return APR_OS2_STATUS(rc);
  45. }
  46. *m = newm;
  47. return APR_SUCCESS;
  48. }
  49. APR_DECLARE(fspr_status_t) fspr_shm_destroy(fspr_shm_t *m)
  50. {
  51. DosFreeMem(m->memblock);
  52. return APR_SUCCESS;
  53. }
  54. APR_DECLARE(fspr_status_t) fspr_shm_remove(const char *filename,
  55. fspr_pool_t *pool)
  56. {
  57. return APR_ENOTIMPL;
  58. }
  59. APR_DECLARE(fspr_status_t) fspr_shm_attach(fspr_shm_t **m,
  60. const char *filename,
  61. fspr_pool_t *pool)
  62. {
  63. int rc;
  64. fspr_shm_t *newm = (fspr_shm_t *)fspr_palloc(pool, sizeof(fspr_shm_t));
  65. char *name = NULL;
  66. ULONG flags = PAG_READ|PAG_WRITE;
  67. newm->pool = pool;
  68. name = fspr_pstrcat(pool, "\\SHAREMEM\\", filename, NULL);
  69. rc = DosGetNamedSharedMem(&(newm->memblock), name, flags);
  70. if (rc) {
  71. return APR_FROM_OS_ERROR(rc);
  72. }
  73. *m = newm;
  74. return APR_SUCCESS;
  75. }
  76. APR_DECLARE(fspr_status_t) fspr_shm_detach(fspr_shm_t *m)
  77. {
  78. int rc = 0;
  79. if (m->memblock) {
  80. rc = DosFreeMem(m->memblock);
  81. }
  82. return APR_FROM_OS_ERROR(rc);
  83. }
  84. APR_DECLARE(void *) fspr_shm_baseaddr_get(const fspr_shm_t *m)
  85. {
  86. return m->memblock;
  87. }
  88. APR_DECLARE(fspr_size_t) fspr_shm_size_get(const fspr_shm_t *m)
  89. {
  90. ULONG flags, size = 0x1000000;
  91. DosQueryMem(m->memblock, &size, &flags);
  92. return size;
  93. }
  94. APR_POOL_IMPLEMENT_ACCESSOR(shm)
  95. APR_DECLARE(fspr_status_t) fspr_os_shm_get(fspr_os_shm_t *osshm,
  96. fspr_shm_t *shm)
  97. {
  98. *osshm = shm->memblock;
  99. return APR_SUCCESS;
  100. }
  101. APR_DECLARE(fspr_status_t) fspr_os_shm_put(fspr_shm_t **m,
  102. fspr_os_shm_t *osshm,
  103. fspr_pool_t *pool)
  104. {
  105. int rc;
  106. fspr_shm_t *newm = (fspr_shm_t *)fspr_palloc(pool, sizeof(fspr_shm_t));
  107. ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
  108. newm->pool = pool;
  109. rc = DosGetSharedMem(&(newm->memblock), flags);
  110. if (rc) {
  111. return APR_FROM_OS_ERROR(rc);
  112. }
  113. *m = newm;
  114. return APR_SUCCESS;
  115. }