X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Ftoken.c;h=f3beb2cf39f6a6b72676c214ec7b12e53ed80450;hp=95eaf7a6f765db9793198c5226be8233f66364af;hb=44bd65fe60fb83fa4c3bbb0d4c679d2b70a096df;hpb=2fa436074ca9a5e87f39b696de832fa2188fcfc6 diff --git a/src/token.c b/src/token.c index 95eaf7a..f3beb2c 100644 --- a/src/token.c +++ b/src/token.c @@ -23,7 +23,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ -#include "php_psi_stdinc.h" +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif #include @@ -32,29 +36,35 @@ #include "token.h" #include "parser.h" -size_t psi_token_alloc_size(size_t token_len, size_t fname_len) { - return sizeof(struct psi_token) + token_len + fname_len + 2; -} +#ifndef PSI_DEBUG_TOKEN_ALLOC +# define PSI_DEBUG_TOKEN_ALLOC 0 +#endif struct psi_token *psi_token_init(token_t token_typ, const char *token_txt, size_t token_len, unsigned col, unsigned line, zend_string *file) { struct psi_token *T; - T = calloc(1, sizeof(*T)); + T = pecalloc(1, sizeof(*T), 1); T->type = token_typ; T->col = col; T->line = line; T->file = zend_string_copy(file); - T->text = zend_string_init(token_txt, token_len, 1); - + T->text = psi_string_init_interned(token_txt, token_len, 1); +#if PSI_DEBUG_TOKEN_ALLOC + fprintf(stderr, "PSI: token_init %p\t", T); + psi_token_dump(NULL, T); +#endif return T; } void psi_token_free(struct psi_token **token_ptr) { if (*token_ptr) { struct psi_token *token = *token_ptr; - +#if PSI_DEBUG_TOKEN_ALLOC + fprintf(stderr, "PSI: token_free %p\t", token); + psi_token_dump(NULL, token); +#endif *token_ptr = NULL; zend_string_release(token->text); zend_string_release(token->file); @@ -63,10 +73,13 @@ void psi_token_free(struct psi_token **token_ptr) { } struct psi_token *psi_token_copy(struct psi_token *src) { - struct psi_token *ptr = malloc(sizeof(*ptr)); + struct psi_token *ptr = pemalloc(sizeof(*ptr), 1); *ptr = *src; - +#if PSI_DEBUG_TOKEN_ALLOC + fprintf(stderr, "PSI: token_copy %p\t", ptr); + psi_token_dump(NULL, src); +#endif ptr->text = zend_string_copy(ptr->text); ptr->file = zend_string_copy(ptr->file); @@ -82,7 +95,7 @@ struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...) { va_list argv; unsigned i; size_t sep_len = sep ? strlen(sep) : 0; - struct psi_token *T = malloc(sizeof(*T)); + struct psi_token *T = pemalloc(sizeof(*T), 1); smart_str text = {0}; va_start(argv, argc); @@ -91,19 +104,21 @@ struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...) { T->type = PSI_T_NAME; T->file = zend_string_copy(T->file); - for (i = 0; i < argc; ++i) { - struct psi_token *arg = va_arg(argv, struct psi_token *); + smart_str_append_ex(&text, T->text, 1); - if (sep_len && text.a) { - smart_str_appendl_ex(&text, sep, sep_len, 1); - } + for (i = 1; i < argc; ++i) { + struct psi_token *arg = va_arg(argv, struct psi_token *); + smart_str_appendl_ex(&text, sep, sep_len, 1); smart_str_append_ex(&text, arg->text, 1); } va_end(argv); T->text = smart_str_extract(&text); +#if PSI_DEBUG_TOKEN_ALLOC + fprintf(stderr, "PSI: token_cat %p\n", T); +#endif return T; } @@ -173,58 +188,58 @@ static inline uint64_t psi_hash(char *digest_buf, ...) uint64_t psi_token_hash(struct psi_token *t, char *digest_buf) { char loc_buf[48]; - sprintf(digest_buf, "%u%u", t->line, t->col); + sprintf(loc_buf, "%u%u", t->line, t->col); return psi_hash(digest_buf, t->file->val, loc_buf, (char *) NULL); } -void psi_token_dump(int fd, struct psi_token *t) +void psi_token_dump(struct psi_dump *dump, struct psi_token *t) { size_t i; - dprintf(fd, "TOKEN %p (%u) ", t, t->type); + PSI_DUMP(dump, "TOKEN %p (%u) ", t, t->type); if (t->type == PSI_T_EOF) { - dprintf(fd, "EOF"); + PSI_DUMP(dump, "EOF"); } else { - dprintf(fd, "\""); + PSI_DUMP(dump, "\""); for (i = 0; i < t->text->len; ++i) { switch (t->text->val[i]) { case '\0': - dprintf(fd, "\\0"); + PSI_DUMP(dump, "\\0"); break; case '\a': - dprintf(fd, "\\a"); + PSI_DUMP(dump, "\\a"); break; case '\b': - dprintf(fd, "\\b"); + PSI_DUMP(dump, "\\b"); break; case '\f': - dprintf(fd, "\\f"); + PSI_DUMP(dump, "\\f"); break; case '\n': - dprintf(fd, "\\n"); + PSI_DUMP(dump, "\\n"); break; case '\r': - dprintf(fd, "\\r"); + PSI_DUMP(dump, "\\r"); break; case '\t': - dprintf(fd, "\\t"); + PSI_DUMP(dump, "\\t"); break; case '\v': - dprintf(fd, "\\v"); + PSI_DUMP(dump, "\\v"); break; case '"': - dprintf(fd, "\\\""); + PSI_DUMP(dump, "\\\""); break; default: if (isprint(t->text->val[i])) { - dprintf(fd, "%c", t->text->val[i]); + PSI_DUMP(dump, "%c", t->text->val[i]); } else { - dprintf(fd, "\\x%02hhX", t->text->val[i]); + PSI_DUMP(dump, "\\x%02hhX", t->text->val[i]); } break; } } - dprintf(fd, "\""); + PSI_DUMP(dump, "\""); } - dprintf(fd, " at col %u in %s on line %u\n", t->col, t->file->val, t->line); + PSI_DUMP(dump, " at col %u in %s on line %u\n", t->col, t->file->val, t->line); }