Re: Structs in CRUNCH

Anton Idukov via Chicken-users <[email protected]>
Newsgroups gmane.lisp.scheme.chicken
Message-ID <CAMJCnqVENQW_StZ8T8CRzigHBMnKmx=XQZaOX3or3xXke5qvew@mail.gmail.com>
Hi, Diogo! Some unscientific answers..
P1.  Yes, it generate





*struct rbuf { uint32_t cap; uint32_t head; uint32_t tail; crunch_u8vector
buf;};*
sizeof(struct rbuf) = 24  on Intel x86_64

But You can define in scm 'byte' type
(define-type Byte (typename uint8_t))






*(define-struct   rbufv1   (cap Byte)   (head Byte)   (tail Byte)   (buf
bytevector))*

sizeof (struct rbufv1) = 16

also if set on packing struct, for example for serialization of data







*(c-declare "#pragma pack(1)")(define-struct  rbufv2  (cap Byte)  (head
Byte)  (tail Byte)  (buf bytevector))(c-declare "#pragma pack()")*

*sizeof (struct rbufv2) = 11 again on Intel x86_64*

P3. As I understand identifier naming in Crunch is similar to C:
underscores,leadchars+[digits]

сб, 13 сент. 2025 г. в 16:08, Diogo via Chicken-users <
[email protected]>:

> Dear CRUNCHers, I am trying to create a simple ring buffer and here are a
> few
> issues I encountered.
>
> The ring buffer is supposed to be very small and only contain bytes. It is
> sufficient to have 4 fields: capacity, head, tail, and byte array.
>
> So I thought this would work:
>
>     (define-struct rbuf
>         (cap char)
>         (head char)
>         (tail char)
>         (buf bytevector))
>
> Problem 1: That generates fields with 32 bits, not chars. I am programming
> a
> microcontroller with CRUNCH and I want to have full control over such
> types.
> The following worked, however:
>
>     (define-struct rbuf
>         (cap (typename uint8_t))
>         (head (typename uint8_t))
>         (tail (typename uint8_t))
>         (buf bytevector))
>
> Problem 2: Besides char not enforcing 8 bit fields, I also had the issue
> that
> I forgot to include (crunch aggregate-types) and `chicken-crunch` gives no
> reasonable error (no string) and simply returns an error code != 0.
>
> Problem 3: Next, I want to define accessors. This is probably my fault,
> but the
> following code
>
>     (define-compound-accessors (struct rbuf)
>         (mk-rbuf cap head tail buf)
>         (cap rbuf-cap)
>         (head rbuf-head rbuf-head!)
>         (tail rbuf-tail rbuf-tail!)
>         (buf rbuf-buf))
>
> generates incorrect/unexpected modifier methods, eg, `rbuf-head!` becomes:
>
>     void rbuf_X2dhead_X21(struct rbuf _s, uint8_t _x) { _s.head = _x; }
>
> Only the copy of the struct in the scope of the function gets modified. I
> was
> expecting to see this:
>
>     void rbuf_X2dhead_X21(struct rbuf *_s, uint8_t _x) { _s->head = _x; }
>
>
> Any feedback is appreciated. Thank you in advance!
>
> Best,
> - Diogo
>
>
cdiogo.scm (text/x-scheme, 794 B)
(import (crunch)
        (crunch c)
        (crunch declarations)
        (crunch aggregate-types))

(define-struct
  rbuf
  (cap char)
  (head char)
  (tail char)
  (buf bytevector))


(define-type Byte (typename uint8_t))
(define-struct
  rbufv1
  (cap Byte)
  (head Byte)
  (tail Byte)
  (buf bytevector))

(c-declare "#pragma pack(1)")
(define-struct
  rbufv2
  (cap Byte)
  (head Byte)
  (tail Byte)
  (buf bytevector))
(c-declare "#pragma pack()")

(c-include <stdio.h>)
(define f (c-lambda () void "printf(\"sizeof rbuf %zu vs rbufv1 %zu vs rbufv2 %zu\\n\",
                                             sizeof(struct rbuf),
                                             sizeof(struct rbufv1),
                                             sizeof(struct rbufv2));"))

(define (main)
  (f))
cdiogo.c (text/x-csrc, 1.5 KB)
/* GENERATED BY CRUNCH */

#define CRUNCH_IMPORT_CRUNCH_AGGREGATE_TYPES
#define CRUNCH_IMPORT_CRUNCH_C
#define CRUNCH_IMPORT_CRUNCH
#define CRUNCH_IMPORT_CRUNCH_DECLARATIONS
#define CRUNCH_IMPORT_CRUNCH_COMPILER_TYPE
#define CRUNCH_IMPORT_MATCHABLE
#define CRUNCH_IMPORT_CHICKEN_MEMORY_REPRESENTATION
#define CRUNCH_IMPORT_CHICKEN_PLIST
#define CRUNCH_IMPORT_CHICKEN_TYPE
#define CRUNCH_IMPORT_CHICKEN_FOREIGN
#define CRUNCH_IMPORT_SCHEME_BASE
#define CRUNCH_IMPORT_CHICKEN_SYNTAX
#define CRUNCH_IMPORT_CHICKEN_BASE
#define CRUNCH_IMPORT_SCHEME_R5RS

#ifndef CRUNCH_H
#include "crunch.h"
#endif

struct rbuf {
uint32_t cap;
uint32_t head;
uint32_t tail;
crunch_u8vector buf;
};
struct rbufv1 {
uint8_t cap;
uint8_t head;
uint8_t tail;
crunch_u8vector buf;
};
#pragma pack(1)
struct rbufv2 {
uint8_t cap;
uint8_t head;
uint8_t tail;
crunch_u8vector buf;
};
#pragma pack()
#include <stdio.h>

void f() {
printf("sizeof rbuf %zu vs rbufv1 %zu vs rbufv2 %zu\n",
                                             sizeof(struct rbuf),
                                             sizeof(struct rbufv1),
                                             sizeof(struct rbufv2));
}

int main(int argc91, char **argv92, char **env93);

crunch_function f___ = (crunch_function)f;
crunch_function main___ = (crunch_function)main;

int main(int argc91, char **argv92, char **env93) {
crunch_main(argc91, argv92, env93);
long r;
// cdiogo.scm:38
// cdiogo.scm:38
f();
r = 0;
goto rl;
rl:
crunch_cleanup();
return r;
}
/* END OF GENERATED CODE */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.