2014-06-18 06:47:01 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2019-01-30 17:23:29 +08:00
|
|
|
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
|
2014-06-18 06:47:01 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2018-11-01 23:20:07 +08:00
|
|
|
| Authors: Dmitry Stogov <dmitry@php.net> |
|
2014-06-18 06:47:01 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ZEND_ARENA_H_
|
|
|
|
#define _ZEND_ARENA_H_
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
|
2019-06-28 16:19:18 +08:00
|
|
|
#ifndef ZEND_TRACK_ARENA_ALLOC
|
|
|
|
|
2014-06-18 06:47:01 +08:00
|
|
|
typedef struct _zend_arena zend_arena;
|
|
|
|
|
|
|
|
struct _zend_arena {
|
|
|
|
char *ptr;
|
|
|
|
char *end;
|
|
|
|
zend_arena *prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
static zend_always_inline zend_arena* zend_arena_create(size_t size)
|
|
|
|
{
|
|
|
|
zend_arena *arena = (zend_arena*)emalloc(size);
|
|
|
|
|
|
|
|
arena->ptr = (char*) arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena));
|
|
|
|
arena->end = (char*) arena + size;
|
|
|
|
arena->prev = NULL;
|
|
|
|
return arena;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_arena_destroy(zend_arena *arena)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
zend_arena *prev = arena->prev;
|
|
|
|
efree(arena);
|
|
|
|
arena = prev;
|
|
|
|
} while (arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void* zend_arena_alloc(zend_arena **arena_ptr, size_t size)
|
|
|
|
{
|
|
|
|
zend_arena *arena = *arena_ptr;
|
|
|
|
char *ptr = arena->ptr;
|
|
|
|
|
|
|
|
size = ZEND_MM_ALIGNED_SIZE(size);
|
|
|
|
|
2014-07-15 19:51:40 +08:00
|
|
|
if (EXPECTED(size <= (size_t)(arena->end - ptr))) {
|
2014-06-18 06:47:01 +08:00
|
|
|
arena->ptr = ptr + size;
|
|
|
|
} else {
|
2015-01-03 17:22:58 +08:00
|
|
|
size_t arena_size =
|
2014-07-15 19:51:40 +08:00
|
|
|
UNEXPECTED((size + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))) > (size_t)(arena->end - (char*) arena)) ?
|
2014-06-18 06:47:01 +08:00
|
|
|
(size + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))) :
|
2014-09-04 18:06:22 +08:00
|
|
|
(size_t)(arena->end - (char*) arena);
|
2014-06-18 06:47:01 +08:00
|
|
|
zend_arena *new_arena = (zend_arena*)emalloc(arena_size);
|
|
|
|
|
|
|
|
ptr = (char*) new_arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena));
|
|
|
|
new_arena->ptr = (char*) new_arena + ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena)) + size;
|
|
|
|
new_arena->end = (char*) new_arena + arena_size;
|
|
|
|
new_arena->prev = arena;
|
2015-01-03 17:22:58 +08:00
|
|
|
*arena_ptr = new_arena;
|
2014-06-18 06:47:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (void*) ptr;
|
|
|
|
}
|
|
|
|
|
2014-06-18 21:09:37 +08:00
|
|
|
static zend_always_inline void* zend_arena_calloc(zend_arena **arena_ptr, size_t count, size_t unit_size)
|
2014-06-18 06:47:01 +08:00
|
|
|
{
|
2014-09-18 17:31:25 +08:00
|
|
|
int overflow;
|
2014-09-07 04:12:37 +08:00
|
|
|
size_t size;
|
|
|
|
void *ret;
|
|
|
|
|
2014-09-18 17:31:25 +08:00
|
|
|
size = zend_safe_address(unit_size, count, 0, &overflow);
|
|
|
|
if (UNEXPECTED(overflow)) {
|
|
|
|
zend_error(E_ERROR, "Possible integer overflow in zend_arena_calloc() (%zu * %zu)", unit_size, count);
|
|
|
|
}
|
2014-09-07 04:12:37 +08:00
|
|
|
ret = zend_arena_alloc(arena_ptr, size);
|
2014-06-18 06:47:01 +08:00
|
|
|
memset(ret, 0, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-18 21:09:37 +08:00
|
|
|
static zend_always_inline void* zend_arena_checkpoint(zend_arena *arena)
|
|
|
|
{
|
|
|
|
return arena->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_arena_release(zend_arena **arena_ptr, void *checkpoint)
|
|
|
|
{
|
|
|
|
zend_arena *arena = *arena_ptr;
|
|
|
|
|
|
|
|
while (UNEXPECTED((char*)checkpoint > arena->end) ||
|
2016-02-09 12:20:11 +08:00
|
|
|
UNEXPECTED((char*)checkpoint <= (char*)arena)) {
|
2014-06-18 21:09:37 +08:00
|
|
|
zend_arena *prev = arena->prev;
|
|
|
|
efree(arena);
|
|
|
|
*arena_ptr = arena = prev;
|
|
|
|
}
|
2016-02-09 12:20:11 +08:00
|
|
|
ZEND_ASSERT((char*)checkpoint > (char*)arena && (char*)checkpoint <= arena->end);
|
2014-06-18 21:09:37 +08:00
|
|
|
arena->ptr = (char*)checkpoint;
|
|
|
|
}
|
|
|
|
|
2019-05-23 18:29:08 +08:00
|
|
|
static zend_always_inline zend_bool zend_arena_contains(zend_arena *arena, void *ptr)
|
|
|
|
{
|
|
|
|
while (arena) {
|
|
|
|
if ((char*)ptr > (char*)arena && (char*)ptr <= arena->ptr) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
arena = arena->prev;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-28 16:19:18 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* Use normal allocations and keep track of them for mass-freeing.
|
|
|
|
* This is intended for use with asan/valgrind. */
|
|
|
|
|
|
|
|
typedef struct _zend_arena zend_arena;
|
|
|
|
|
|
|
|
struct _zend_arena {
|
|
|
|
void **ptr;
|
|
|
|
void **end;
|
|
|
|
struct _zend_arena *prev;
|
|
|
|
void *ptrs[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ZEND_TRACKED_ARENA_SIZE 1000
|
|
|
|
|
|
|
|
static zend_always_inline zend_arena *zend_arena_create(size_t _size)
|
|
|
|
{
|
|
|
|
zend_arena *arena = (zend_arena*) emalloc(
|
|
|
|
sizeof(zend_arena) + sizeof(void *) * ZEND_TRACKED_ARENA_SIZE);
|
|
|
|
arena->ptr = &arena->ptrs[0];
|
|
|
|
arena->end = &arena->ptrs[ZEND_TRACKED_ARENA_SIZE];
|
|
|
|
arena->prev = NULL;
|
|
|
|
return arena;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_arena_destroy(zend_arena *arena)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
zend_arena *prev = arena->prev;
|
|
|
|
void **ptr;
|
|
|
|
for (ptr = arena->ptrs; ptr < arena->ptr; ptr++) {
|
|
|
|
efree(*ptr);
|
|
|
|
}
|
|
|
|
efree(arena);
|
|
|
|
arena = prev;
|
|
|
|
} while (arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void *zend_arena_alloc(zend_arena **arena_ptr, size_t size)
|
|
|
|
{
|
|
|
|
zend_arena *arena = *arena_ptr;
|
|
|
|
if (arena->ptr == arena->end) {
|
|
|
|
*arena_ptr = zend_arena_create(0);
|
|
|
|
(*arena_ptr)->prev = arena;
|
|
|
|
arena = *arena_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *arena->ptr++ = emalloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void* zend_arena_calloc(zend_arena **arena_ptr, size_t count, size_t unit_size)
|
|
|
|
{
|
|
|
|
int overflow;
|
|
|
|
size_t size;
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
size = zend_safe_address(unit_size, count, 0, &overflow);
|
|
|
|
if (UNEXPECTED(overflow)) {
|
|
|
|
zend_error(E_ERROR, "Possible integer overflow in zend_arena_calloc() (%zu * %zu)", unit_size, count);
|
|
|
|
}
|
|
|
|
ret = zend_arena_alloc(arena_ptr, size);
|
|
|
|
memset(ret, 0, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void* zend_arena_checkpoint(zend_arena *arena)
|
|
|
|
{
|
|
|
|
return arena->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline void zend_arena_release(zend_arena **arena_ptr, void *checkpoint)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
zend_arena *arena = *arena_ptr;
|
|
|
|
zend_arena *prev = arena->prev;
|
|
|
|
while (1) {
|
|
|
|
if (arena->ptr == (void **) checkpoint) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (arena->ptr == arena->ptrs) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
arena->ptr--;
|
|
|
|
efree(*arena->ptr);
|
|
|
|
}
|
|
|
|
efree(arena);
|
|
|
|
*arena_ptr = prev;
|
|
|
|
ZEND_ASSERT(*arena_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static zend_always_inline zend_bool zend_arena_contains(zend_arena *arena, void *ptr)
|
|
|
|
{
|
|
|
|
/* TODO: Dummy */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-06-18 06:47:01 +08:00
|
|
|
#endif /* _ZEND_ARENA_H_ */
|