| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <common/buffer-view.h> |
| 19 | #include <common/dynamic-buffer.h> |
| 20 | #include <common/macros.h> |
| 21 | #include <common/error.h> |
| 22 | #include <assert.h> |
| 23 | |
| 24 | struct lttng_buffer_view lttng_buffer_view_from_view( |
| 25 | const struct lttng_buffer_view *src, size_t offset, |
| 26 | ptrdiff_t len) |
| 27 | { |
| 28 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; |
| 29 | |
| 30 | assert(src); |
| 31 | |
| 32 | if (offset > src->size) { |
| 33 | ERR("Attempt to create buffer view with invalid offset"); |
| 34 | goto end; |
| 35 | } |
| 36 | |
| 37 | if (len != -1 && len > (src->size - offset)) { |
| 38 | ERR("Attempt to create buffer view with invalid length"); |
| 39 | goto end; |
| 40 | } |
| 41 | |
| 42 | view.data = src->data + offset; |
| 43 | view.size = len == -1 ? (src->size - offset) : len; |
| 44 | end: |
| 45 | return view; |
| 46 | } |
| 47 | |
| 48 | struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( |
| 49 | const struct lttng_dynamic_buffer *src, size_t offset, |
| 50 | ptrdiff_t len) |
| 51 | { |
| 52 | struct lttng_buffer_view view = { .data = NULL, .size = 0 }; |
| 53 | |
| 54 | assert(src); |
| 55 | |
| 56 | if (offset > src->size) { |
| 57 | ERR("Attempt to create buffer view with invalid offset"); |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | if (len != -1 && len > (src->size - offset)) { |
| 62 | ERR("Attempt to create buffer view with invalid length"); |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | view.data = src->data + offset; |
| 67 | view.size = len == -1 ? (src->size - offset) : len; |
| 68 | end: |
| 69 | return view; |
| 70 | } |