shm.c 3.6 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 "apr_general.h"
  17. #include "apr_shm.h"
  18. #include "apr_errno.h"
  19. #include "apr_lib.h"
  20. #include "apr_strings.h"
  21. #include "apr_portable.h"
  22. struct apr_shm_t {
  23. apr_pool_t *pool;
  24. void *memblock;
  25. };
  26. APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
  27. apr_size_t reqsize,
  28. const char *filename,
  29. apr_pool_t *pool)
  30. {
  31. int rc;
  32. apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
  33. char *name = NULL;
  34. ULONG flags = PAG_COMMIT|PAG_READ|PAG_WRITE;
  35. newm->pool = pool;
  36. if (filename) {
  37. name = apr_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(apr_status_t) apr_shm_destroy(apr_shm_t *m)
  50. {
  51. DosFreeMem(m->memblock);
  52. return APR_SUCCESS;
  53. }
  54. APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
  55. apr_pool_t *pool)
  56. {
  57. return APR_ENOTIMPL;
  58. }
  59. APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
  60. const char *filename,
  61. apr_pool_t *pool)
  62. {
  63. int rc;
  64. apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_shm_t));
  65. char *name = NULL;
  66. ULONG flags = PAG_READ|PAG_WRITE;
  67. newm->pool = pool;
  68. name = apr_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(apr_status_t) apr_shm_detach(apr_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 *) apr_shm_baseaddr_get(const apr_shm_t *m)
  85. {
  86. return m->memblock;
  87. }
  88. APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_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(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
  96. apr_shm_t *shm)
  97. {
  98. *osshm = shm->memblock;
  99. return APR_SUCCESS;
  100. }
  101. APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
  102. apr_os_shm_t *osshm,
  103. apr_pool_t *pool)
  104. {
  105. int rc;
  106. apr_shm_t *newm = (apr_shm_t *)apr_palloc(pool, sizeof(apr_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. }