Add autotools build
[babeltrace.git] / types / integer.c
CommitLineData
fc93b2bd 1/*
ccd7e1c8 2 * integer.c
fc93b2bd 3 *
ccd7e1c8 4 * BabelTrace - Integer Type Converter
fc93b2bd 5 *
ccd7e1c8 6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 7 *
ccd7e1c8
MD
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
fc93b2bd 14 *
ccd7e1c8
MD
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
fc93b2bd
MD
17 */
18
19#include <babeltrace/compiler.h>
698f0fe4 20#include <babeltrace/align.h>
448d3cc7 21#include <babeltrace/types.h>
fc93b2bd
MD
22#include <stdint.h>
23
698f0fe4 24size_t integer_copy(unsigned char *dest, const struct format *fdest,
fc93b2bd
MD
25 const unsigned char *src, const struct format *fsrc,
26 const struct type_class *type_class)
27{
28 struct type_class_integer *int_class =
29 container_of(type_class, struct type_class_integer, p);
30
31 if (!int_class->signedness) {
32 uint64_t v;
33
bed864a7
MD
34 v = fsrc->uint_read(src, int_class);
35 return fdest->uint_write(dest, int_class, v);
fc93b2bd
MD
36 } else {
37 int64_t v;
38
bed864a7
MD
39 v = fsrc->int_read(src, int_class);
40 return fdest->int_write(dest, int_class, v);
fc93b2bd
MD
41 }
42}
698f0fe4 43
90b676d7
MD
44void integer_type_free(struct type_class_integer *int_class)
45{
46 g_free(int_class);
47}
48
49static void _integer_type_free(struct type_class *type_class)
50{
51 struct type_class_integer *int_class =
52 container_of(type_class, struct type_class_integer, p);
53 integer_type_free(int_class);
54}
55
0a46062b 56struct type_class_integer *integer_type_new(const char *name,
0a46062b
MD
57 size_t len, int byte_order,
58 int signedness,
59 size_t alignment)
698f0fe4 60{
0a46062b 61 struct type_class_integer *int_class;
698f0fe4
MD
62 int ret;
63
698f0fe4
MD
64 int_class = g_new(struct type_class_integer, 1);
65 int_class->p.name = g_quark_from_string(name);
66 int_class->p.alignment = alignment;
67 int_class->p.copy = integer_copy;
90b676d7 68 int_class->p.free = _integer_type_free;
698f0fe4
MD
69 int_class->len = len;
70 int_class->byte_order = byte_order;
71 int_class->signedness = signedness;
0a46062b 72 if (int_class->p.name) {
bed864a7 73 ret = ctf_register_type(&int_class->p);
0a46062b
MD
74 if (ret) {
75 g_free(int_class);
76 return NULL;
77 }
78 }
79 return int_class;
698f0fe4 80}
This page took 0.025597 seconds and 4 git commands to generate.