apr_buckets_mmap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
  2. * applicable.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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_buckets.h"
  17. #if APR_HAS_MMAP
  18. static apr_status_t mmap_bucket_read(apr_bucket *b, const char **str,
  19. apr_size_t *length, apr_read_type_e block)
  20. {
  21. apr_bucket_mmap *m = b->data;
  22. apr_status_t ok;
  23. void *addr;
  24. if (!m->mmap) {
  25. /* the apr_mmap_t was already cleaned up out from under us */
  26. return APR_EINVAL;
  27. }
  28. ok = apr_mmap_offset(&addr, m->mmap, b->start);
  29. if (ok != APR_SUCCESS) {
  30. return ok;
  31. }
  32. *str = addr;
  33. *length = b->length;
  34. return APR_SUCCESS;
  35. }
  36. static apr_status_t mmap_bucket_cleanup(void *data)
  37. {
  38. /* the apr_mmap_t is about to disappear out from under us, so we
  39. * have no choice but to pretend it doesn't exist anymore. the
  40. * refcount is now useless because there's nothing to refer to
  41. * anymore. so the only valid action on any remaining referrer
  42. * is to delete it. no more reads, no more anything. */
  43. apr_bucket_mmap *m = data;
  44. m->mmap = NULL;
  45. return APR_SUCCESS;
  46. }
  47. static void mmap_bucket_destroy(void *data)
  48. {
  49. apr_bucket_mmap *m = data;
  50. if (apr_bucket_shared_destroy(m)) {
  51. if (m->mmap) {
  52. apr_pool_cleanup_kill(m->mmap->cntxt, m, mmap_bucket_cleanup);
  53. apr_mmap_delete(m->mmap);
  54. }
  55. apr_bucket_free(m);
  56. }
  57. }
  58. /*
  59. * XXX: are the start and length arguments useful?
  60. */
  61. APU_DECLARE(apr_bucket *) apr_bucket_mmap_make(apr_bucket *b, apr_mmap_t *mm,
  62. apr_off_t start,
  63. apr_size_t length)
  64. {
  65. apr_bucket_mmap *m;
  66. m = apr_bucket_alloc(sizeof(*m), b->list);
  67. m->mmap = mm;
  68. apr_pool_cleanup_register(mm->cntxt, m, mmap_bucket_cleanup,
  69. apr_pool_cleanup_null);
  70. b = apr_bucket_shared_make(b, m, start, length);
  71. b->type = &apr_bucket_type_mmap;
  72. return b;
  73. }
  74. APU_DECLARE(apr_bucket *) apr_bucket_mmap_create(apr_mmap_t *mm,
  75. apr_off_t start,
  76. apr_size_t length,
  77. apr_bucket_alloc_t *list)
  78. {
  79. apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
  80. APR_BUCKET_INIT(b);
  81. b->free = apr_bucket_free;
  82. b->list = list;
  83. return apr_bucket_mmap_make(b, mm, start, length);
  84. }
  85. static apr_status_t mmap_bucket_setaside(apr_bucket *b, apr_pool_t *p)
  86. {
  87. apr_bucket_mmap *m = b->data;
  88. apr_mmap_t *mm = m->mmap;
  89. apr_mmap_t *new_mm;
  90. apr_status_t ok;
  91. if (!mm) {
  92. /* the apr_mmap_t was already cleaned up out from under us */
  93. return APR_EINVAL;
  94. }
  95. /* shortcut if possible */
  96. if (apr_pool_is_ancestor(mm->cntxt, p)) {
  97. return APR_SUCCESS;
  98. }
  99. /* duplicate apr_mmap_t into new pool */
  100. ok = apr_mmap_dup(&new_mm, mm, p);
  101. if (ok != APR_SUCCESS) {
  102. return ok;
  103. }
  104. /* decrement refcount on old apr_bucket_mmap */
  105. mmap_bucket_destroy(m);
  106. /* create new apr_bucket_mmap pointing to new apr_mmap_t */
  107. apr_bucket_mmap_make(b, new_mm, b->start, b->length);
  108. return APR_SUCCESS;
  109. }
  110. APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_mmap = {
  111. "MMAP", 5, APR_BUCKET_DATA,
  112. mmap_bucket_destroy,
  113. mmap_bucket_read,
  114. mmap_bucket_setaside,
  115. apr_bucket_shared_split,
  116. apr_bucket_shared_copy
  117. };
  118. #endif