Convert get_ and put_ to functions

This shaves off about 1 kB of flash usage from my client application.
It is also more type safe, which is nice.
This commit is contained in:
jonath.re@gmail.com 2022-07-29 03:38:37 +02:00 committed by Jonathan Reichelt Gjertsen
parent 22fae7da9c
commit 70c75cb2db

View File

@ -50,29 +50,48 @@
#endif #endif
#endif #endif
#define get_1(m) \ static uint8_t get_1(nmbs_t *m) {
(m)->msg.buf[(m)->msg.buf_idx]; \ uint8_t result = m->msg.buf[m->msg.buf_idx];
(m)->msg.buf_idx++ m->msg.buf_idx++;
#define put_1(m, b) \ return result;
(m)->msg.buf[(m)->msg.buf_idx] = (b); \ }
(m)->msg.buf_idx++
#define discard_1(m) (m)->msg.buf_idx++ static void put_1(nmbs_t *m, uint8_t b) {
m->msg.buf[(m)->msg.buf_idx] = b;
m->msg.buf_idx++;
}
static void discard_1(nmbs_t *m) {
m->msg.buf_idx++;
}
#ifdef NMBS_BIG_ENDIAN #ifdef NMBS_BIG_ENDIAN
#define get_2(m) \
(*(uint16_t*) ((m)->msg.buf + (m)->msg.buf_idx)); \ static uint16_t get_2(nmbs_t *m) {
(m)->msg.buf_idx += 2 uint16_t result = (*(uint16_t*) (m->msg.buf + m->msg.buf_idx));
#define put_2(m, w) \ m->msg.buf_idx += 2;
(*(uint16_t*) ((m)->msg.buf + (m)->msg.buf_idx)) = (w); \ return result;
(m)->msg.buf_idx += 2 }
static void put_2(nmbs_t *m, uint16_t w) {
(*(uint16_t*) (m->msg.buf + m->msg.buf_idx)) = w;
m->msg.buf_idx += 2;
}
#else #else
#define get_2(m) \
((uint16_t) ((m)->msg.buf[(m)->msg.buf_idx + 1])) | (((uint16_t) (m)->msg.buf[(m)->msg.buf_idx] << 8)); \ static uint16_t get_2(nmbs_t *m) {
(m)->msg.buf_idx += 2 uint16_t result = ((uint16_t) (m->msg.buf[m->msg.buf_idx + 1])) | (((uint16_t) m->msg.buf[m->msg.buf_idx] << 8));
#define put_2(m, w) \ m->msg.buf_idx += 2;
(m)->msg.buf[(m)->msg.buf_idx] = ((uint8_t) ((((uint16_t) (w)) & 0xFF00) >> 8)); \ return result;
(m)->msg.buf[(m)->msg.buf_idx + 1] = ((uint8_t) (((uint16_t) (w)) & 0x00FF)); \ }
(m)->msg.buf_idx += 2
static void put_2(nmbs_t *m, uint16_t w) {
m->msg.buf[m->msg.buf_idx] = ((uint8_t) ((((uint16_t) (w)) & 0xFF00) >> 8));
m->msg.buf[m->msg.buf_idx + 1] = ((uint8_t) (((uint16_t) (w)) & 0x00FF));
m->msg.buf_idx += 2;
}
#endif #endif