mysqlnd_block_alloc.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Andrey Hristov <andrey@php.net> |
  14. | Ulf Wendel <uw@php.net> |
  15. | Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "mysqlnd.h"
  20. #include "mysqlnd_block_alloc.h"
  21. #include "mysqlnd_debug.h"
  22. #include "mysqlnd_priv.h"
  23. /* {{{ mysqlnd_mempool_get_chunk */
  24. static void *
  25. mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, size_t size)
  26. {
  27. DBG_ENTER("mysqlnd_mempool_get_chunk");
  28. DBG_RETURN(zend_arena_alloc(&pool->arena, size));
  29. }
  30. /* }}} */
  31. /* {{{ mysqlnd_mempool_create */
  32. PHPAPI MYSQLND_MEMORY_POOL *
  33. mysqlnd_mempool_create(size_t arena_size)
  34. {
  35. zend_arena * arena;
  36. MYSQLND_MEMORY_POOL * ret;
  37. DBG_ENTER("mysqlnd_mempool_create");
  38. arena = zend_arena_create(MAX(arena_size, ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))));
  39. ret = zend_arena_alloc(&arena, sizeof(MYSQLND_MEMORY_POOL));
  40. ret->arena = arena;
  41. ret->checkpoint = NULL;
  42. ret->get_chunk = mysqlnd_mempool_get_chunk;
  43. DBG_RETURN(ret);
  44. }
  45. /* }}} */
  46. /* {{{ mysqlnd_mempool_destroy */
  47. PHPAPI void
  48. mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)
  49. {
  50. DBG_ENTER("mysqlnd_mempool_destroy");
  51. /* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
  52. zend_arena_destroy(pool->arena);
  53. DBG_VOID_RETURN;
  54. }
  55. /* }}} */
  56. /* {{{ mysqlnd_mempool_save_state */
  57. PHPAPI void
  58. mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)
  59. {
  60. DBG_ENTER("mysqlnd_mempool_save_state");
  61. pool->checkpoint = zend_arena_checkpoint(pool->arena);
  62. DBG_VOID_RETURN;
  63. }
  64. /* }}} */
  65. /* {{{ mysqlnd_mempool_restore_state */
  66. PHPAPI void
  67. mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)
  68. {
  69. DBG_ENTER("mysqlnd_mempool_restore_state");
  70. #if ZEND_DEBUG
  71. ZEND_ASSERT(pool->checkpoint);
  72. #endif
  73. if (pool->checkpoint) {
  74. zend_arena_release(&pool->arena, pool->checkpoint);
  75. pool->checkpoint = NULL;
  76. }
  77. DBG_VOID_RETURN;
  78. }
  79. /* }}} */