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 __APPLE__
8#include <stddef.h>
9typedef ptrdiff_t MR_int64_t;
10typedef size_t MR_uint64_t;
11#else
12#include <stdint.h>
13typedef int64_t MR_int64_t;
14typedef uint64_t MR_uint64_t;
15#endif
16
17
18typedef enum MR_PassBy
19{
20 MR_PassBy_DefaultConstruct, // Default-construct this parameter, the associated pointer must be null.
21 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.
22 MR_PassBy_Move, // Move the object into the function. The input object remains alive and still needs to be manually destroyed after.
23 MR_PassBy_DefaultArgument, // If this function has a default argument value for this parameter, uses that; illegal otherwise. The associated pointer must be null.
24 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.
26
28MRC_API void *MR_Alloc(size_t num_bytes);
29
31MRC_API void MR_Free(void *ptr);
32
37MRC_API void *MR_AllocArray(size_t num_bytes);
38
40MRC_API void MR_FreeArray(void *ptr);
41
42// The deprecation attribute.
43#if !defined(MRC_DEPRECATED) && !defined(MRC_DEPRECATED_REASON)
44# if defined(__cplusplus) // C++:
45# ifdef __has_cpp_attribute
46# if __has_cpp_attribute(deprecated)
47# define MRC_DEPRECATED [[deprecated]]
48# ifdef _MSC_VER
49# 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.
50# else
51# define MRC_DEPRECATED_REASON(str) [[deprecated(str)]]
52# endif
53# endif
54# endif
55# elif defined(_MSC_VER) // C in MSVC. It has a buggy `__has_c_attribute`, so needs to be special-cased.
56# 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.
57# define MRC_DEPRECATED [[deprecated]]
58# 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.
59# else // Fall back to the non-standard syntax.
60# define MRC_DEPRECATED __declspec(deprecated)
61# 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.
62# endif
63# else // C not in MSVC:
64# 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.
65# if __has_c_attribute(deprecated)
66# define MRC_DEPRECATED [[deprecated]]
67# define MRC_DEPRECATED_REASON(str) [[deprecated(str)]]
68# endif
69# endif
70# if !defined(MRC_DEPRECATED) && defined(__GNUC__)
71# define MRC_DEPRECATED __attribute__((__deprecated__))
72# define MRC_DEPRECATED_REASON(str) __attribute__((__deprecated__(str)))
73# endif
74# endif
75# ifndef MRC_DEPRECATED // If nothing above has worked, just expand to nothing.
76# define MRC_DEPRECATED
77# endif
78# ifndef MRC_DEPRECATED_REASON
79# define MRC_DEPRECATED_REASON(str) MRC_DEPRECATED
80# endif
81#endif
82
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:13
MR_PassBy
Definition common.h:19
@ MR_PassBy_NoObject
Definition common.h:24
@ MR_PassBy_DefaultConstruct
Definition common.h:20
@ MR_PassBy_DefaultArgument
Definition common.h:23
@ MR_PassBy_Move
Definition common.h:22
@ MR_PassBy_Copy
Definition common.h:21
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:14
#define MRC_API
Definition exports.h:11