MeshLib C Docs
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1#pragma once
2
3#include <MRCMisc/exports.h>
4
5#include <stddef.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#ifdef __APPLE__
12#include <stddef.h>
13typedef ptrdiff_t MR_int64_t;
14typedef size_t MR_uint64_t;
15#else
16#include <stdint.h>
17typedef int64_t MR_int64_t;
18typedef uint64_t MR_uint64_t;
19#endif
20
21
22typedef enum MR_PassBy
23{
24 MR_PassBy_DefaultConstruct, // Default-construct this parameter, the associated pointer must be null.
25 MR_PassBy_Copy, // Copy the object into the function. For most types this doesn't modify the input object, so feel free to cast away constness from it if needed.
26 MR_PassBy_Move, // Move the object into the function. The input object remains alive and still needs to be manually destroyed after.
27 MR_PassBy_MoveAndDestroy, // Same as `Move`, but also destroy the pointer after moving from it.
28 MR_PassBy_DefaultArgument, // If this function has a default argument value for this parameter, uses that; illegal otherwise. The associated pointer must be null.
29 MR_PassBy_NoObject, // This is used to pass no object to the function (functions supporting this will document this fact). This is used e.g. for C++ `std::optional<T>` parameters.
31
33MRC_API void *MR_Alloc(size_t num_bytes);
34
36MRC_API void MR_Free(void *ptr);
37
42MRC_API void *MR_AllocArray(size_t num_bytes);
43
45MRC_API void MR_FreeArray(void *ptr);
46
47// The deprecation attribute.
48#if !defined(MRC_DEPRECATED) && !defined(MRC_DEPRECATED_REASON)
49# if defined(__cplusplus) // C++:
50# ifdef __has_cpp_attribute
51# if __has_cpp_attribute(deprecated)
52# define MRC_DEPRECATED [[deprecated]]
53# ifdef _MSC_VER
54# define MRC_DEPRECATED_REASON(str) [[deprecated("is deprecated: " str)]] // When using this form, MSVC just dumps the entity name and the message, without telling you that it is a deprecation warning. So we add this part ourselves.
55# else
56# define MRC_DEPRECATED_REASON(str) [[deprecated(str)]]
57# endif
58# endif
59# endif
60# elif defined(_MSC_VER) // C in MSVC. It has a buggy `__has_c_attribute`, so needs to be special-cased.
61# if _MSC_VER >= 1937 && __STDC_VERSION__ >= 202312 // Funnily enough, MSVC doesn't even define `__STDC_VERSION__` in `/std:clatest` mode in 1936, but does define it in if you pass `/std:c17`. 1937 does define it in both cases (through the value for C23 is supposed to be one less than what it reports, `202311`). This also coincides with `[[deprecated]]` getting implemented in C.
62# define MRC_DEPRECATED [[deprecated]]
63# define MRC_DEPRECATED_REASON(str) [[deprecated("is deprecated: " str)]] // When using this form, MSVC just dumps the entity name and the message, without telling you that it is a deprecation warning. So we add this part ourselves.
64# else // Fall back to the non-standard syntax.
65# define MRC_DEPRECATED __declspec(deprecated)
66# define MRC_DEPRECATED_REASON(str) __declspec(deprecated("is deprecated: " str)) // When using this form, MSVC just dumps the entity name and the message, without telling you that it is a deprecation warning. So we add this part ourselves.
67# endif
68# else // C not in MSVC:
69# if defined(__has_c_attribute) && (__STDC_VERSION__ >= 202311 || !defined(__GNUC__)) // Don't trust the `__has_c_attribute()` alone, as new attributes might warn when used in old C, even if the compiler reports them as supported. So if we have `__GNUC__` and an old C version, prefer the non-standard syntax instead. If the C version is old, but this isn't `__GNUC__`, then we have no syntax to fall back to, so we just use this one.
70# if __has_c_attribute(deprecated)
71# define MRC_DEPRECATED [[deprecated]]
72# define MRC_DEPRECATED_REASON(str) [[deprecated(str)]]
73# endif
74# endif
75# if !defined(MRC_DEPRECATED) && defined(__GNUC__)
76# define MRC_DEPRECATED __attribute__((__deprecated__))
77# define MRC_DEPRECATED_REASON(str) __attribute__((__deprecated__(str)))
78# endif
79# endif
80# ifndef MRC_DEPRECATED // If nothing above has worked, just expand to nothing.
81# define MRC_DEPRECATED
82# endif
83# ifndef MRC_DEPRECATED_REASON
84# define MRC_DEPRECATED_REASON(str) MRC_DEPRECATED
85# endif
86#endif
87
88#ifdef __cplusplus
89} // extern "C"
90#endif
MRC_API void * MR_AllocArray(size_t num_bytes)
MRC_API void MR_FreeArray(void *ptr)
Deallocates memory that was previously allocated with MR_AllocArray(). Does nothing if the pointer is...
int64_t MR_int64_t
Definition common.h:17
MR_PassBy
Definition common.h:23
@ MR_PassBy_NoObject
Definition common.h:29
@ MR_PassBy_DefaultConstruct
Definition common.h:24
@ MR_PassBy_MoveAndDestroy
Definition common.h:27
@ MR_PassBy_DefaultArgument
Definition common.h:28
@ MR_PassBy_Move
Definition common.h:26
@ MR_PassBy_Copy
Definition common.h:25
MRC_API void * MR_Alloc(size_t num_bytes)
Allocates n bytes of memory, which can then be freed using MR_Free().
MRC_API void MR_Free(void *ptr)
Deallocates memory that was previously allocated with MR_Alloc(). Does nothing if the pointer is null...
uint64_t MR_uint64_t
Definition common.h:18
#define MRC_API
Definition include/MRCMisc/exports.h:11