20#ifndef KERNEL_UTILITIES_LIB_H
21#define KERNEL_UTILITIES_LIB_H
26#include "pedigree/kernel/compiler.h"
27#include "pedigree/kernel/processor/types.h"
30#if UTILITY_LINUX && defined(__APPLE__)
35#if X86_COMMON || HOSTED_X86_COMMON || (UTILITY_LINUX && (defined(__i386__) || defined(__x86_64__)))
39#if defined(__cplusplus) && !defined(__APPLE__)
40#define NOTHROW noexcept
50#define StringLength(x) (IS_CONSTANT(x) ? __builtin_strlen((x)) : _StringLength(x))
56#define BoundedStringLength(x, y) \
57 (IS_CONSTANT(x) ? __builtin_strlen((x)) : _BoundedStringLength(x, y))
59#define ConstStringLength(x) static_assert(IS_CONSTANT((x))), __builtin_strlen((x))
62EXPORTED_PUBLIC
size_t _StringLength(
const char* buf)
PURE;
63EXPORTED_PUBLIC
size_t _BoundedStringLength(
const char* buf,
size_t maxlen)
PURE;
64EXPORTED_PUBLIC
char* StringCopy(
char* dest,
const char* src);
65EXPORTED_PUBLIC
char* StringCopyN(
char* dest,
const char* src,
size_t len);
66EXPORTED_PUBLIC
int StringCompare(
const char* p1,
const char* p2)
PURE;
67EXPORTED_PUBLIC
int StringCompareN(
const char* p1,
const char* p2,
size_t n)
PURE;
68EXPORTED_PUBLIC
int StringCompareNOffset(
const char* p1,
const char* p2,
size_t n,
70EXPORTED_PUBLIC
char* StringConcat(
char* dest,
const char* src);
71EXPORTED_PUBLIC
char* StringConcatN(
char* dest,
const char* src,
size_t n);
72EXPORTED_PUBLIC
char* StringFind(
const char* str,
int target)
PURE;
73EXPORTED_PUBLIC
char* StringReverseFind(
const char* str,
int target)
PURE;
74EXPORTED_PUBLIC
int StringContains(
const char* str,
const char* search)
PURE;
75EXPORTED_PUBLIC
int StringContainsN(
const char* str,
size_t len,
const char* search,
77EXPORTED_PUBLIC
int VStringFormat(
char* buf,
const char* fmt, va_list arg);
78EXPORTED_PUBLIC
int StringFormat(
char* buf,
const char* fmt, ...)
FORMAT(printf, 2, 3);
79EXPORTED_PUBLIC
unsigned long StringToUnsignedLong(const
char* nptr,
char** endptr,
int base);
85EXPORTED_PUBLIC
int StringMatch(const
char* p1, const
char* p2)
PURE;
86EXPORTED_PUBLIC
int StringMatchN(const
char* p1, const
char* p2,
size_t n)
PURE;
87EXPORTED_PUBLIC
int StringMatchNOffset(const
char* p1, const
char* p2,
size_t n,
92#if !defined(__cplusplus) || defined(IMPLEMENTING_LOG_FORMAT_FUNCTIONS)
94EXPORTED_PUBLIC
int Debugf(
const char* fmt, ...)
FORMAT(printf, 1, 2);
95EXPORTED_PUBLIC
int Noticef(const
char* fmt, ...)
FORMAT(printf, 1, 2);
96EXPORTED_PUBLIC
int Warningf(const
char* fmt, ...)
FORMAT(printf, 1, 2);
97EXPORTED_PUBLIC
int Errorf(const
char* fmt, ...)
FORMAT(printf, 1, 2);
98EXPORTED_PUBLIC
int Fatalf(const
char* fmt, ...)
FORMAT(printf, 1, 2) NORETURN;
104EXPORTED_PUBLIC
int StringCompareCase(
const char* s1,
const char* s2,
int sensitive,
size_t length,
108EXPORTED_PUBLIC
void* ByteSet(
void* buf,
int c,
size_t len);
109EXPORTED_PUBLIC
void* WordSet(
void* buf,
int c,
size_t len);
110EXPORTED_PUBLIC
void* DoubleWordSet(
void* buf,
unsigned int c,
size_t len);
111EXPORTED_PUBLIC
void* QuadWordSet(
void* buf,
unsigned long long c,
size_t len);
112EXPORTED_PUBLIC
void* ForwardMemoryCopy(
void* s1,
const void* s2,
size_t n);
113EXPORTED_PUBLIC
void* MemoryCopy(
void* s1,
const void* s2,
size_t n);
114EXPORTED_PUBLIC
int MemoryCompare(
const void* p1,
const void* p2,
size_t len)
PURE;
117EXPORTED_PUBLIC
const char* SDirectoryName(
const char* path,
char* buf,
size_t buflen)
PURE;
118EXPORTED_PUBLIC
const char* SBaseName(
const char* path,
char* buf,
size_t buflen)
PURE;
119EXPORTED_PUBLIC
const char* DirectoryName(
const char* path)
PURE;
120EXPORTED_PUBLIC
const char* BaseName(
const char* path)
PURE;
123#if !(UTILITY_LINUX && defined(__APPLE__))
124EXPORTED_PUBLIC
int isspace(
int c) NOTHROW;
125EXPORTED_PUBLIC
int isupper(
int c) NOTHROW;
126EXPORTED_PUBLIC
int islower(
int c) NOTHROW;
127EXPORTED_PUBLIC
int isdigit(
int c) NOTHROW;
128EXPORTED_PUBLIC
int isalpha(
int c) NOTHROW;
132void random_seed(uint64_t seed);
133EXPORTED_PUBLIC uint64_t random_next(
void);
137EXPORTED_PUBLIC
size_t hardware_random_bytes(
void* buffer,
size_t length);
140EXPORTED_PUBLIC
size_t secure_random_bytes(
void* buffer,
size_t length);
141EXPORTED_PUBLIC
int secure_random_seed(
const void* buffer,
size_t length);
143EXPORTED_PUBLIC
char toUpper(
char c)
PURE;
144EXPORTED_PUBLIC
char toLower(
char c)
PURE;
145EXPORTED_PUBLIC
int max(
size_t a,
size_t b)
PURE;
146EXPORTED_PUBLIC
int min(
size_t a,
size_t b)
PURE;
149#if !(UTILITY_LINUX || HOSTED)
153EXPORTED_PUBLIC
void free(
void*)
C_NOTHROW;
156EXPORTED_PUBLIC
size_t nextCharacter(
const char* s,
size_t i);
157EXPORTED_PUBLIC
size_t prevCharacter(
const char* s,
size_t i);
160uint8_t checksum(
const uint8_t* pMemory,
size_t sMemory);
163uint16_t checksum16(
const uint16_t* pMemory,
size_t sMemory);
166uint32_t checksum32(
const uint32_t* pMemory,
size_t sMemory);
169uint32_t checksum32_naive(
const uint32_t* pMemory,
size_t sMemory);
172uint32_t checksumPage(uintptr_t address);
175uint32_t elfHash(
const char* buffer,
size_t length);
178uint32_t jenkinsHash(
const char* buffer,
size_t length);
181EXPORTED_PUBLIC uint32_t spookyHash(
const char* buffer,
size_t length);
182EXPORTED_PUBLIC uint64_t spookyHash64(
const char* buffer,
size_t length);
183EXPORTED_PUBLIC
void spookyHash128(
const char* buffer,
size_t length, uint64_t* h1, uint64_t* h2);
186EXPORTED_PUBLIC
int overlaps(
const void* s1,
const void* s2,
size_t n)
PURE;
192#include "pedigree/kernel/utilities/cpp.h"
#define FORMAT(type, idx, first)