source: thirdparty/SZ/sz/src/DynamicByteArray.c @ 2c47b73

Revision 2c47b73, 1.7 KB checked in by Hal Finkel <hfinkel@…>, 6 years ago (diff)

more work on adding SZ (latest version)

  • Property mode set to 100644
Line 
1/**
2 *  @file DynamicByteArray.c
3 *  @author Sheng Di
4 *  @date May, 2016
5 *  @brief Dynamic Byte Array
6 *  (C) 2015 by Mathematics and Computer Science (MCS), Argonne National Laboratory.
7 *      See COPYRIGHT in top-level directory.
8 */
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13#include "DynamicByteArray.h"
14
15void new_DBA(DynamicByteArray **dba, size_t cap) {
16                *dba = (DynamicByteArray *)malloc(sizeof(DynamicByteArray));
17        (*dba)->size = 0;
18        (*dba)->capacity = cap;
19        (*dba)->array = (unsigned char*)malloc(sizeof(unsigned char)*cap);
20    }
21
22void convertDBAtoBytes(DynamicByteArray *dba, unsigned char** bytes)
23{
24        size_t size = dba->size;
25        if(size>0)
26                *bytes = (unsigned char*)malloc(size * sizeof(unsigned char));
27        else
28                *bytes = NULL;
29        memcpy(*bytes, dba->array, size*sizeof(unsigned char)); 
30}
31
32void free_DBA(DynamicByteArray *dba)
33{
34        free(dba->array);
35        free(dba);
36}
37
38unsigned char getDBA_Data(DynamicByteArray *dba, size_t pos)
39{
40        if(pos>=dba->size)
41        {
42                printf("Error: wrong position of DBA (impossible case unless bugs elsewhere in the code?).\n");
43                exit(0);
44        }
45        return dba->array[pos];
46}
47
48void addDBA_Data(DynamicByteArray *dba, unsigned char value)
49{
50        if(dba->size==dba->capacity)
51        {
52                dba->capacity = dba->capacity << 1;
53                dba->array = (unsigned char *)realloc(dba->array, dba->capacity*sizeof(unsigned char));
54        }
55        dba->array[dba->size] = value;
56        dba->size ++;
57}
58
59void memcpyDBA_Data(DynamicByteArray *dba, unsigned char* data, size_t length)
60{
61        if(dba->size + length > dba->capacity)
62        {
63                dba->capacity = dba->size + length;
64                dba->array = (unsigned char *)realloc(dba->array, dba->capacity*sizeof(unsigned char));
65        }
66        memcpy(&(dba->array[dba->size]), data, length);
67        dba->size += length;
68}
Note: See TracBrowser for help on using the repository browser.