apr_buckets_heap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #define APR_WANT_MEMFUNC
  18. #include "apr_want.h"
  19. static apr_status_t heap_bucket_read(apr_bucket *b, const char **str,
  20. apr_size_t *len, apr_read_type_e block)
  21. {
  22. apr_bucket_heap *h = b->data;
  23. *str = h->base + b->start;
  24. *len = b->length;
  25. return APR_SUCCESS;
  26. }
  27. static void heap_bucket_destroy(void *data)
  28. {
  29. apr_bucket_heap *h = data;
  30. if (apr_bucket_shared_destroy(h)) {
  31. (*h->free_func)(h->base);
  32. apr_bucket_free(h);
  33. }
  34. }
  35. /* Warning: if you change this function, be sure to
  36. * change apr_bucket_pool_make() too! */
  37. APU_DECLARE(apr_bucket *) apr_bucket_heap_make(apr_bucket *b, const char *buf,
  38. apr_size_t length,
  39. void (*free_func)(void *data))
  40. {
  41. apr_bucket_heap *h;
  42. h = apr_bucket_alloc(sizeof(*h), b->list);
  43. if (!free_func) {
  44. h->alloc_len = length;
  45. h->base = apr_bucket_alloc(h->alloc_len, b->list);
  46. if (h->base == NULL) {
  47. apr_bucket_free(h);
  48. return NULL;
  49. }
  50. h->free_func = apr_bucket_free;
  51. memcpy(h->base, buf, length);
  52. }
  53. else {
  54. /* XXX: we lose the const qualifier here which indicates
  55. * there's something screwy with the API...
  56. */
  57. h->base = (char *) buf;
  58. h->alloc_len = length;
  59. h->free_func = free_func;
  60. }
  61. b = apr_bucket_shared_make(b, h, 0, length);
  62. b->type = &apr_bucket_type_heap;
  63. return b;
  64. }
  65. APU_DECLARE(apr_bucket *) apr_bucket_heap_create(const char *buf,
  66. apr_size_t length,
  67. void (*free_func)(void *data),
  68. apr_bucket_alloc_t *list)
  69. {
  70. apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
  71. APR_BUCKET_INIT(b);
  72. b->free = apr_bucket_free;
  73. b->list = list;
  74. return apr_bucket_heap_make(b, buf, length, free_func);
  75. }
  76. APU_DECLARE_DATA const apr_bucket_type_t apr_bucket_type_heap = {
  77. "HEAP", 5, APR_BUCKET_DATA,
  78. heap_bucket_destroy,
  79. heap_bucket_read,
  80. apr_bucket_setaside_noop,
  81. apr_bucket_shared_split,
  82. apr_bucket_shared_copy
  83. };