| 1 |
/* sysprof-capture-util-private.h |
| 2 |
* |
| 3 |
* Copyright 2019 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* Subject to the terms and conditions of this license, each copyright holder |
| 17 |
* and contributor hereby grants to those receiving rights under this license |
| 18 |
* a perpetual, worldwide, non-exclusive, no-charge, royalty-free, |
| 19 |
* irrevocable (except for failure to satisfy the conditions of this license) |
| 20 |
* patent license to make, have made, use, offer to sell, sell, import, and |
| 21 |
* otherwise transfer this software, where such license applies only to those |
| 22 |
* patent claims, already acquired or hereafter acquired, licensable by such |
| 23 |
* copyright holder or contributor that are necessarily infringed by: |
| 24 |
* |
| 25 |
* (a) their Contribution(s) (the licensed copyrights of copyright holders |
| 26 |
* and non-copyrightable additions of contributors, in source or binary |
| 27 |
* form) alone; or |
| 28 |
* |
| 29 |
* (b) combination of their Contribution(s) with the work of authorship to |
| 30 |
* which such Contribution(s) was added by such copyright holder or |
| 31 |
* contributor, if, at the time the Contribution is added, such addition |
| 32 |
* causes such combination to be necessarily infringed. The patent license |
| 33 |
* shall not apply to any other combinations which include the |
| 34 |
* Contribution. |
| 35 |
* |
| 36 |
* Except as expressly stated above, no rights or licenses from any copyright |
| 37 |
* holder or contributor is granted under this license, whether expressly, by |
| 38 |
* implication, estoppel or otherwise. |
| 39 |
* |
| 40 |
* DISCLAIMER |
| 41 |
* |
| 42 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 43 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 44 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 45 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
| 46 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 47 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 48 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 49 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 50 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 51 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 52 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 53 |
* |
| 54 |
* SPDX-License-Identifier: BSD-2-Clause-Patent |
| 55 |
*/ |
| 56 |
|
| 57 |
#pragma once |
| 58 |
|
| 59 |
#ifdef __linux__ |
| 60 |
# include <sys/sendfile.h> |
| 61 |
#endif |
| 62 |
|
| 63 |
#include <errno.h> |
| 64 |
#include <stdlib.h> |
| 65 |
#include <string.h> |
| 66 |
#include <unistd.h> |
| 67 |
|
| 68 |
#ifndef TEMP_FAILURE_RETRY |
| 69 |
#define TEMP_FAILURE_RETRY(expression) \ |
| 70 |
({ long int __result; \ |
| 71 |
do { __result = (long int) (expression); } \ |
| 72 |
while (__result == -1L && errno == EINTR); \ |
| 73 |
__result; }) |
| 74 |
#endif |
| 75 |
|
| 76 |
static inline void * |
| 77 |
sysprof_malloc0 (size_t size) |
| 78 |
{ |
| 79 |
void *ptr = malloc (size); |
| 80 |
if (ptr == NULL) |
| 81 |
return NULL; |
| 82 |
memset (ptr, 0, size); |
| 83 |
return ptr; |
| 84 |
} |
| 85 |
|
| 86 |
#ifdef __linux__ |
| 87 |
# define _sysprof_getpagesize() getpagesize() |
| 88 |
# define _sysprof_pread(a,b,c,d) pread(a,b,c,d) |
| 89 |
# define _sysprof_pwrite(a,b,c,d) pwrite(a,b,c,d) |
| 90 |
# define _sysprof_write(a,b,c) write(a,b,c) |
| 91 |
# define _sysprof_getpid() getpid() |
| 92 |
# define _sysprof_sendfile(a,b,c,d) sendfile(a,b,c,d) |
| 93 |
#else |
| 94 |
size_t _sysprof_getpagesize (void); |
| 95 |
ssize_t _sysprof_pread (int fd, |
| 96 |
void *buf, |
| 97 |
size_t count, |
| 98 |
off_t offset); |
| 99 |
ssize_t _sysprof_pwrite (int fd, |
| 100 |
const void *buf, |
| 101 |
size_t count, |
| 102 |
off_t offset); |
| 103 |
ssize_t _sysprof_write (int fd, |
| 104 |
const void *buf, |
| 105 |
size_t count); |
| 106 |
int32_t _sysprof_getpid (void); |
| 107 |
ssize_t _sysprof_sendfile (int out_fd, |
| 108 |
int in_fd, |
| 109 |
off_t *offset, |
| 110 |
size_t count); |
| 111 |
#endif |
| 112 |
|
| 113 |
#ifdef HAVE_STRLCPY |
| 114 |
# define _sysprof_strlcpy(d,s,ds) strlcpy(d,s,ds) |
| 115 |
#else |
| 116 |
size_t _sysprof_strlcpy (char *dest, |
| 117 |
const char *src, |
| 118 |
size_t dest_size); |
| 119 |
#endif |
| 120 |
|
| 121 |
#ifdef HAVE_REALLOCARRAY |
| 122 |
# define _sysprof_reallocarray(p,m,n) reallocarray(p,m,n) |
| 123 |
#else |
| 124 |
void *_sysprof_reallocarray (void *ptr, |
| 125 |
size_t m, |
| 126 |
size_t n); |
| 127 |
#endif |