forked from ursinusnetworks/Chatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraylist.c
More file actions
32 lines (27 loc) · 778 Bytes
/
arraylist.c
File metadata and controls
32 lines (27 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "arraylist.h"
#define INITIAL_SIZE 1024
void ArrayListBuf_init(struct ArrayListBuf* b) {
b->buff = (char*)malloc(INITIAL_SIZE);
b->capacity = INITIAL_SIZE;
b->N = 0;
}
void ArrayListBuf_free(struct ArrayListBuf* b) {
free(b->buff);
}
void ArrayListBuf_doubleCapacity(struct ArrayListBuf* b) {
char* newBuff = (char*)malloc(b->capacity*2);
memcpy(newBuff, b->buff, b->capacity);
free(b->buff);
b->buff = newBuff;
b->capacity *= 2;
}
void ArrayListBuf_push(struct ArrayListBuf* b, char* s, int len) {
while (b->N + len > b->capacity) {
ArrayListBuf_doubleCapacity(b);
}
memcpy(b->buff+b->N, s, len);
b->N += len;
}