lib: fix avl tree lookup
[project/udebug.git] / priv.h
1 #ifndef __UDEBUG_UTIL_H
2 #define __UDEBUG_UTIL_H
3
4 #include <libubox/blobmsg.h>
5 #include "udebug.h"
6
7 #define UDEBUG_TIMEOUT 1000
8
9 struct udebug_hdr {
10 uint32_t ring_size;
11 uint32_t data_size;
12
13 uint32_t format;
14 uint32_t sub_format;
15
16 uintptr_t flags[8 / sizeof(uintptr_t)];
17 uintptr_t notify;
18
19 uint32_t head_hi;
20 uint32_t head;
21 uint32_t data_head;
22 uint32_t data_used;
23 };
24
25 enum udebug_client_msg_type {
26 CL_MSG_RING_ADD,
27 CL_MSG_RING_REMOVE,
28 CL_MSG_RING_NOTIFY,
29 CL_MSG_GET_HANDLE,
30 CL_MSG_RING_GET,
31 CL_MSG_ERROR,
32 };
33
34 struct udebug_client_msg {
35 uint8_t type;
36 uint8_t _pad[3];
37 uint32_t id;
38 union {
39 struct {
40 uint32_t ring_size, data_size;
41 };
42 uint32_t notify_mask;
43 };
44 } __attribute__((packed, aligned(4)));
45
46 static inline struct udebug_ptr *
47 udebug_ring_ptr(struct udebug_hdr *hdr, uint32_t idx)
48 {
49 struct udebug_ptr *ring = (struct udebug_ptr *)&hdr[1];
50 return &ring[idx & (hdr->ring_size - 1)];
51 }
52
53 static inline void *udebug_buf_ptr(struct udebug_buf *buf, uint32_t ofs)
54 {
55 return buf->data + (ofs & (buf->data_size - 1));
56 }
57
58 int udebug_id_cmp(const void *k1, const void *k2, void *ptr);
59 __hidden int udebug_buf_open(struct udebug_buf *buf, int fd, uint32_t ring_size, uint32_t data_size);
60 __hidden struct udebug_client_msg *__udebug_poll(struct udebug *ctx, int *fd, bool wait);
61 __hidden void udebug_send_msg(struct udebug *ctx, struct udebug_client_msg *msg,
62 struct blob_attr *meta, int fd);
63 __hidden void __udebug_disconnect(struct udebug *ctx, bool reconnect);
64
65 static inline int32_t u32_sub(uint32_t a, uint32_t b)
66 {
67 return a - b;
68 }
69
70 static inline int32_t u32_max(uint32_t a, uint32_t b)
71 {
72 return u32_sub(a, b) > 0 ? a : b;
73 }
74
75 static inline void u32_set(void *ptr, uint32_t val)
76 {
77 volatile uint32_t *v = ptr;
78 *v = val;
79 }
80
81 static inline uint32_t u32_get(void *ptr)
82 {
83 volatile uint32_t *v = ptr;
84 return *v;
85 }
86
87 #endif