Skip to main content
rm tag from title
Link
svick
  • 24.5k
  • 4
  • 53
  • 89

Reference counted dynamic byte array in C

Tweeted twitter.com/#!/StackCodeReview/status/358232170529177600
Fixed formatting
Source Link
Lstor
  • 2.7k
  • 1
  • 21
  • 32

#include "byte_array.h"

#include "byte_array.h"

#define NEW_CAPABILITY a->capability * 2

static void zero_init_bytes(struct byte_array *a, 
                            uint32_t offset, 
                            uint32_t nmemb)
{
        uint8_t *bytes = a->bytes;
        bytes += offset;
        memset(bytes, 0, nmemb);
}

struct byte_array *byte_array_create(uint32_t capability)
{
        struct byte_array *result = assert_malloc(sizeof(*result));
        result->refs = 1;
        result->length = 0;
        result->capability = capability;
        result->bytes = assert_calloc(capability, sizeof(uint8_t));

        return result;
}

void byte_array_push(struct byte_array *a, uint8_t input)
{
        assert(a != NULL);

        if (a->length == a->capability) {
                void *tmp = assert_realloc(a->bytes, NEW_CAPABILITY);

                a->bytes = tmp;

                zero_init_bytes(
                        a, 
                        a->capability, 
                        NEW_CAPABILITY - a->capability
                );

                a->capability = NEW_CAPABILITY;
        }

        a->bytes[a->length++] = input;
}

void byte_array_incref(struct byte_array *a)
{
        assert(a != NULL);

        a->refs++;
}

void byte_array_decref(struct byte_array *a)
{
        assert(a != NULL);

        if (--a->refs != 0) return;

        free(a->bytes);
        free(a);
}

#include "byte_array.h"

#define NEW_CAPABILITY a->capability * 2

static void zero_init_bytes(struct byte_array *a, 
                            uint32_t offset, 
                            uint32_t nmemb)
{
        uint8_t *bytes = a->bytes;
        bytes += offset;
        memset(bytes, 0, nmemb);
}

struct byte_array *byte_array_create(uint32_t capability)
{
        struct byte_array *result = assert_malloc(sizeof(*result));
        result->refs = 1;
        result->length = 0;
        result->capability = capability;
        result->bytes = assert_calloc(capability, sizeof(uint8_t));

        return result;
}

void byte_array_push(struct byte_array *a, uint8_t input)
{
        assert(a != NULL);

        if (a->length == a->capability) {
                void *tmp = assert_realloc(a->bytes, NEW_CAPABILITY);

                a->bytes = tmp;

                zero_init_bytes(
                        a, 
                        a->capability, 
                        NEW_CAPABILITY - a->capability
                );

                a->capability = NEW_CAPABILITY;
        }

        a->bytes[a->length++] = input;
}

void byte_array_incref(struct byte_array *a)
{
        assert(a != NULL);

        a->refs++;
}

void byte_array_decref(struct byte_array *a)
{
        assert(a != NULL);

        if (--a->refs != 0) return;

        free(a->bytes);
        free(a);
}
#include "byte_array.h"

#define NEW_CAPABILITY a->capability * 2

static void zero_init_bytes(struct byte_array *a, 
                            uint32_t offset, 
                            uint32_t nmemb)
{
        uint8_t *bytes = a->bytes;
        bytes += offset;
        memset(bytes, 0, nmemb);
}

struct byte_array *byte_array_create(uint32_t capability)
{
        struct byte_array *result = assert_malloc(sizeof(*result));
        result->refs = 1;
        result->length = 0;
        result->capability = capability;
        result->bytes = assert_calloc(capability, sizeof(uint8_t));

        return result;
}

void byte_array_push(struct byte_array *a, uint8_t input)
{
        assert(a != NULL);

        if (a->length == a->capability) {
                void *tmp = assert_realloc(a->bytes, NEW_CAPABILITY);

                a->bytes = tmp;

                zero_init_bytes(
                        a, 
                        a->capability, 
                        NEW_CAPABILITY - a->capability
                );

                a->capability = NEW_CAPABILITY;
        }

        a->bytes[a->length++] = input;
}

void byte_array_incref(struct byte_array *a)
{
        assert(a != NULL);

        a->refs++;
}

void byte_array_decref(struct byte_array *a)
{
        assert(a != NULL);

        if (--a->refs != 0) return;

        free(a->bytes);
        free(a);
}
Add more descriptive headline
Link
rzetterberg
  • 375
  • 1
  • 4
  • 13

Dynamic Reference counted dynamic byte array in C

Source Link
rzetterberg
  • 375
  • 1
  • 4
  • 13
Loading