cbase coverage


File: src/type/private/ptr_type.c
Date: 2024-07-26 20:57:32
Lines:
13/13
100.0%
Functions:
4/4
100.0%
Branches:
0/0
-%

Line Branch Exec Source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stddef.h>
5
6 #include "type.h"
7
8 /**
9 * @brief Return a copy of this pointer
10 *
11 * @param s The pointer
12 * @return void* The copy of this pointer
13 */
14 31 void* ptr_copy(const void* p) {
15 31 return (void*)p;
16 }
17
18 /**
19 * @brief Return a representation of this pointer in a rope_t
20 *
21 * @param p The pointer
22 * @return rope_t* The representation of this pointer
23 */
24 41 rope_t* ptr_repr(const void* p) {
25 41 char* str = (char*)malloc(
26 sizeof(*str) *
27 41 (sizeof(long long) * 2 - __builtin_clzll((ptrdiff_t)p) / 4 + 5)
28 );
29 41 sprintf(str, "\"%p\"", p);
30 41 rope_t* r = rope_create_with(str);
31 41 free(str);
32 41 return r;
33 }
34
35 /**
36 * @brief Return a hash of this pointer
37 *
38 * @param p The pointer
39 * @return uint64_t The hash of this pointer
40 */
41 45 uint64_t ptr_hash(const void* p) {
42 45 return (uint64_t)(ptrdiff_t)p;
43 }
44
45 /**
46 * @brief Compare two pointers
47 *
48 * @param p1 The first pointer to compare
49 * @param p2 The second pointer to compare
50 * @return int p1 - p2
51 */
52 1031 int ptr_cmp(const void* p1, const void* p2) {
53 1031 return p1 - p2;
54 }
55
56 /// @brief Pointer type
57 const type_t* ptr_type = &(type_t){
58 .identifier = "ptr",
59 .destroy = NULL,
60 .copy = ptr_copy,
61 .repr = ptr_repr,
62 .hash = ptr_hash,
63 .cmp = ptr_cmp
64 };
65