aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/buf.c
blob: 90e33adaa16b2b9d25e2efeb5f5ee334e796fdac (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "buf.h"
#include "log.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

struct buf {
	uint32_t size;
	const void *data;
};

int buf_create(struct buf **_buf, const void *data, uint32_t size)
{
	struct buf *buf = malloc(sizeof(struct buf));
	if (!buf) {
		log_errno("malloc");
		return -1;
	}

	buf->data = data;
	buf->size = size;

	*_buf = buf;
	return 0;
}

int buf_create_from_string(struct buf **buf, const char *str)
{
	return buf_create(buf, str, strlen(str) + 1);
}

void buf_destroy(struct buf *buf)
{
	free(buf);
}

uint32_t buf_get_size(const struct buf *buf)
{
	return buf->size;
}

const void *buf_get_data(const struct buf *buf)
{
	return buf->data;
}