~ufirst/phpredis/igbinary

« back to all changes in this revision

Viewing changes to igbinary.c

  • Committer: Teddy Grenman
  • Date: 2012-07-23 19:20:49 UTC
  • Revision ID: git-v1:c35d48f3d14794373b2ef89a6d79020bb7418d7f
Realloc buffer at end of serialization to the real size.

Also, avoid extra copy in serssion serialization.

Show diffs side-by-side

added added

removed removed

Lines of Context:
378
378
/* {{{ int igbinary_serialize_ex(uint8_t**, size_t*, zval*, igbinary_memory_manager*) */
379
379
IGBINARY_API int igbinary_serialize_ex(uint8_t **ret, size_t *ret_len, zval *z, struct igbinary_memory_manager *memory_manager TSRMLS_DC) {
380
380
        struct igbinary_serialize_data igsd;
 
381
        uint8_t *tmpbuf;
381
382
 
382
383
        if (igbinary_serialize_data_init(&igsd, Z_TYPE_P(z) != IS_OBJECT && Z_TYPE_P(z) != IS_ARRAY, memory_manager TSRMLS_CC)) {
383
384
                zend_error(E_WARNING, "igbinary_serialize: cannot init igsd");
401
402
                return 1;
402
403
        }
403
404
 
 
405
        /* shrink buffer to the real length, ignore errors */
 
406
        tmpbuf = (uint8_t *) igsd.mm.realloc(igsd.buffer, igsd.buffer_size, igsd.mm.context);
 
407
        if (tmpbuf != NULL) {
 
408
                igsd.buffer = tmpbuf;
 
409
        }
 
410
 
404
411
        /* Set return values */
405
412
        *ret_len = igsd.buffer_size - 1;
406
413
        *ret = igsd.buffer;
483
490
PS_SERIALIZER_ENCODE_FUNC(igbinary)
484
491
{
485
492
        struct igbinary_serialize_data igsd;
486
 
        char *s;
 
493
        uint8_t *tmpbuf;
487
494
 
488
495
        if (igbinary_serialize_data_init(&igsd, false, NULL TSRMLS_CC)) {
489
496
                zend_error(E_WARNING, "igbinary_serialize: cannot init igsd");
501
508
                return FAILURE;
502
509
        }
503
510
 
504
 
        s = estrndup((char*)igsd.buffer, igsd.buffer_size);
505
 
        if (s == NULL) {
 
511
        if (igbinary_serialize8(&igsd, 0 TSRMLS_CC) != 0) {
 
512
                igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
506
513
                return FAILURE;
507
514
        }
508
515
 
509
 
        *newstr = s;
 
516
        /* shrink buffer to the real length, ignore errors */
 
517
        tmpbuf = (uint8_t *)igsd.mm.realloc(igsd.buffer, igsd.buffer_size, igsd.mm.context);
 
518
        if (tmpbuf != NULL) {
 
519
                igsd.buffer = tmpbuf;
 
520
        }
 
521
 
 
522
        *newstr = (char *)igsd.buffer;
510
523
        if (newlen) {
511
 
                *newlen = igsd.buffer_size;
 
524
                *newlen = igsd.buffer_size - 1;
512
525
        }
513
526
 
514
 
        igbinary_serialize_data_deinit(&igsd, 1 TSRMLS_CC);
 
527
        igbinary_serialize_data_deinit(&igsd, 0 TSRMLS_CC);
515
528
 
516
529
        return SUCCESS;
517
530
}