summaryrefslogtreecommitdiffstats
path: root/bytecode/slice.h
blob: 4f4624c066f78475686b90a7692333ba572dd8bf (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
#ifndef _SLICE_H_
#define _SLICE_H_

#include <assert.h>
#include <stddef.h>

#define DEFINE_SLICE(t)                                                \
	typedef struct {                                               \
		size_t size;                                           \
		t *buf;                                                \
	} t##_slice;                                                   \
	                                                               \
	static inline t##_slice                                        \
	t##_slice2(t##_slice slice, size_t i, size_t j)                \
	{                                                              \
		assert(i <= slice.size && j <= slice.size && i <= j);  \
		return (t##_slice) {                                   \
			.size = j - i,                                 \
			.buf = slice.buf + i,                          \
	       	};                                                     \
	}                                                              \
	                                                               \
	static inline t##_slice                                        \
	t##_slice1(t##_slice slice, size_t i)                          \
	{                                                              \
		assert(i <= slice.size);                               \
		return (t##_slice) {                                   \
			.size = slice.size - i,                        \
			.buf = slice.buf + i,                          \
		};                                                     \
	}

#endif