@@ -52,11 +52,11 @@ /** @file */ /** * @brief A result of a comparison. */ -typedef enum OFComparisonResult { +typedef enum { /** The left object is smaller than the right */ OFOrderedAscending = -1, /** Both objects are equal */ OFOrderedSame = 0, /** The left object is bigger than the right */ @@ -73,13 +73,13 @@ */ typedef OFComparisonResult (^OFComparator)(id _Nonnull left, id _Nonnull right); #endif /** - * @brief An enum for storing endianess. + * @brief An enum for representing endianess. */ -typedef enum OFByteOrder { +typedef enum { /** Most significant byte first (big endian) */ OFByteOrderBigEndian, /** Least significant byte first (little endian) */ OFByteOrderLittleEndian, /** Native byte order of the system */ @@ -93,17 +93,16 @@ /** * @struct OFRange OFObject.h ObjFW/OFObject.h * * @brief A range. */ -struct OF_BOXABLE OFRange { +typedef struct OF_BOXABLE { /** The start of the range */ size_t location; /** The length of the range */ size_t length; -}; -typedef struct OFRange OFRange; +} OFRange; /** * @brief Creates a new OFRange. * * @param start The starting index of the range @@ -145,17 +144,16 @@ /** * @struct OFPoint OFObject.h ObjFW/OFObject.h * * @brief A point. */ -struct OF_BOXABLE OFPoint { +typedef struct OF_BOXABLE { /** The x coordinate of the point */ float x; /** The y coordinate of the point */ float y; -}; -typedef struct OFPoint OFPoint; +} OFPoint; /** * @brief Creates a new OFPoint. * * @param x The x coordinate of the point @@ -192,17 +190,16 @@ /** * @struct OFSize OFObject.h ObjFW/OFObject.h * * @brief A size. */ -struct OF_BOXABLE OFSize { +typedef struct OF_BOXABLE { /** The width of the size */ float width; /** The height of the size */ float height; -}; -typedef struct OFSize OFSize; +} OFSize; /** * @brief Creates a new OFSize. * * @param width The width of the size @@ -239,17 +236,16 @@ /** * @struct OFRect OFObject.h ObjFW/OFObject.h * * @brief A rectangle. */ -struct OF_BOXABLE OFRect { +typedef struct OF_BOXABLE { /** The point from where the rectangle originates */ OFPoint origin; /** The size of the rectangle */ OFSize size; -}; -typedef struct OFRect OFRect; +} OFRect; /** * @brief Creates a new OFRect. * * @param x The x coordinate of the top left corner of the rectangle @@ -285,10 +281,60 @@ if (!OFSizeEqual(rect1.size, rect2.size)) return false; return true; } + +/** + * @brief Adds the specified byte to the hash. + * + * @param hash A pointer to a hash to add the byte to + * @param byte The byte to add to the hash + */ +static OF_INLINE void +OFHashAdd(unsigned long *_Nonnull hash, unsigned char byte) +{ + uint32_t tmp = (uint32_t)*hash; + + tmp += byte; + tmp += tmp << 10; + tmp ^= tmp >> 6; + + *hash = tmp; +} + +/** + * @brief Adds the specified hash to the hash. + * + * @param hash A pointer to a hash to add the hash to + * @param otherHash The hash to add to the hash + */ +static OF_INLINE void +OFHashAddHash(unsigned long *_Nonnull hash, unsigned long otherHash) +{ + OFHashAdd(hash, (otherHash >> 24) & 0xFF); + OFHashAdd(hash, (otherHash >> 16) & 0xFF); + OFHashAdd(hash, (otherHash >> 8) & 0xFF); + OFHashAdd(hash, otherHash & 0xFF); +} + +/** + * @brief Finalizes the specified hash. + * + * @param hash A pointer to the hash to finalize + */ +static OF_INLINE void +OFHashFinalize(unsigned long *_Nonnull hash) +{ + uint32_t tmp = (uint32_t)*hash; + + tmp += tmp << 3; + tmp ^= tmp >> 11; + tmp += tmp << 15; + + *hash = tmp; +} static const size_t OFNotFound = SIZE_MAX; #ifdef __OBJC__ @class OFMethodSignature; @@ -581,14 +627,13 @@ * * Derived classes can override this to execute their own code when the class * is unloaded. * * @warning This is not supported by the Apple runtime and currently only - * called by the ObjFW runtime when objc_unregister_class() or - * objc_exit() has been called! + * called by the ObjFW runtime when @ref objc_deinit has been called! * In the future, this might also be called by the ObjFW runtime when - * the class is part of a plugin that has been unloaded. + * the class is part of a plugin that is being unloaded. */ + (void)unload; /** * @brief A method which is called the moment before the first call to the class @@ -1334,10 +1379,17 @@ * @brief Returns 64 bit or non-cryptographical randomness. * * @return 64 bit or non-cryptographical randomness */ extern uint64_t OFRandom64(void); + +/** + * @brief Initializes the specified hash. + * + * @param hash A pointer to the hash to initialize + */ +extern void OFHashInit(unsigned long *_Nonnull hash); #ifdef __cplusplus } #endif OF_ASSUME_NONNULL_END