Open Broadcaster Software
Free, open source software for live streaming and recording
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bmem.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #pragma once
18 
19 #include "c99defs.h"
20 #include "base.h"
21 #include <wchar.h>
22 #include <string.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
29  void *(*malloc)(size_t);
30  void *(*realloc)(void *, size_t);
31  void (*free)(void *);
32 };
33 
34 EXPORT void base_set_allocator(struct base_allocator *defs);
35 
36 EXPORT void *bmalloc(size_t size);
37 EXPORT void *brealloc(void *ptr, size_t size);
38 EXPORT void bfree(void *ptr);
39 
40 EXPORT int base_get_alignment(void);
41 
42 EXPORT long bnum_allocs(void);
43 
44 EXPORT void *bmemdup(const void *ptr, size_t size);
45 
46 static inline void *bzalloc(size_t size)
47 {
48  void *mem = bmalloc(size);
49  if (mem)
50  memset(mem, 0, size);
51  return mem;
52 }
53 
54 static inline char *bstrdup_n(const char *str, size_t n)
55 {
56  char *dup;
57  if (!str)
58  return NULL;
59 
60  dup = (char*)bmemdup(str, n+1);
61  dup[n] = 0;
62 
63  return dup;
64 }
65 
66 static inline wchar_t *bwstrdup_n(const wchar_t *str, size_t n)
67 {
68  wchar_t *dup;
69  if (!str)
70  return NULL;
71 
72  dup = (wchar_t*)bmemdup(str, (n+1) * sizeof(wchar_t));
73  dup[n] = 0;
74 
75  return dup;
76 }
77 
78 static inline char *bstrdup(const char *str)
79 {
80  if (!str)
81  return NULL;
82 
83  return bstrdup_n(str, strlen(str));
84 }
85 
86 static inline wchar_t *bwstrdup(const wchar_t *str)
87 {
88  if (!str)
89  return NULL;
90 
91  return bwstrdup_n(str, wcslen(str));
92 }
93 
94 #ifdef __cplusplus
95 }
96 #endif
EXPORT void * bmemdup(const void *ptr, size_t size)
EXPORT void base_set_allocator(struct base_allocator *defs)
Definition: bmem.h:28
EXPORT void * brealloc(void *ptr, size_t size)
EXPORT void * bmalloc(size_t size)
#define EXPORT
Definition: c99defs.h:49
void(* free)(void *)
Definition: bmem.h:31
EXPORT long bnum_allocs(void)
EXPORT void bfree(void *ptr)
EXPORT int base_get_alignment(void)