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_DefaultArgument, // If this function has a default argument value for this parameter, uses that; illegal otherwise. The associated pointer must be null.
28 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.
30
32MRC_API void *MR_Alloc(size_t num_bytes);
33
35MRC_API void MR_Free(void *ptr);
36
41MRC_API void *MR_AllocArray(size_t num_bytes);
42
44MRC_API void MR_FreeArray(void *ptr);
45
46// The deprecation attribute.
47#if !defined(MRC_DEPRECATED) && !defined(MRC_DEPRECATED_REASON)
48# if defined(__cplusplus) // C++:
49# ifdef __has_cpp_attribute
50# if __has_cpp_attribute(deprecated)
51# define MRC_DEPRECATED [[deprecated]]
52# ifdef _MSC_VER
53# 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.
54# else
55# define MRC_DEPRECATED_REASON(str) [[deprecated(str)]]
56# endif
57# endif
58# endif
59# elif defined(_MSC_VER) // C in MSVC. It has a buggy `__has_c_attribute`, so needs to be special-cased.
60# 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.
61# define MRC_DEPRECATED [[deprecated]]
62# 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.
63# else // Fall back to the non-standard syntax.
64# define MRC_DEPRECATED __declspec(deprecated)
65# 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.
66# endif
67# else // C not in MSVC:
68# 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.
69# if __has_c_attribute(deprecated)
70# define MRC_DEPRECATED [[deprecated]]
71# define MRC_DEPRECATED_REASON(str) [[deprecated(str)]]
72# endif
73# endif
74# if !defined(MRC_DEPRECATED) && defined(__GNUC__)
75# define MRC_DEPRECATED __attribute__((__deprecated__))
76# define MRC_DEPRECATED_REASON(str) __attribute__((__deprecated__(str)))
77# endif
78# endif
79# ifndef MRC_DEPRECATED // If nothing above has worked, just expand to nothing.
80# define MRC_DEPRECATED
81# endif
82# ifndef MRC_DEPRECATED_REASON
83# define MRC_DEPRECATED_REASON(str) MRC_DEPRECATED
84# endif
85#endif
86
87#ifdef __cplusplus
88} // extern "C"
89#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:28
@ MR_PassBy_DefaultConstruct
Definition common.h:24
@ MR_PassBy_DefaultArgument
Definition common.h:27
@ 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 exports.h:11