P
pozzugno
I'm programming on an embedded platform that lacks malloc()/free().
I can use only static allocation.
I'm writing two half-duplex protocols (protocol A and protocol B)
that will be implemented on several physical channels (at the moment,
two serial ports). Both protocols are of "slave" type: the program
waits for a request, processes it and answers. The payload (of request
and answer) should be packed in a different packet structure for protocol
A and B, but the payload are the same.
So I have one main function for each protocol:
int protocol_A(int fd, const void *buffer, size_t size);
int protocol_B(int fd, const void *buffer, size_t size);
And I have one function for each request:
int rqst_on(int fd, const void *payload, size_t size);
int rqst_off(int fd, const void *payload, size_t size);
...
fd is the file descriptor where the request was received and where
to write the answer (if any).
protocol_A() and protocol_B() parse the receiving buffer and call
the right request handler, passing the payload and its size.
Now the problem is how to handle the answer that should be packed
differently for each protocol (and I don't want to write two
request handlers, one for A and one for B).
I have two solutions, but both have some bad things.
The first is to allocate the answer buffer inside protocol_x():
int protocol_A(int fd, const void *buffer, size_t size) {
unsigned char answer[100];
size_t size = sizeof(answer);
...
n = rqst_on(fd, payload, size, answer, anssize);
/* Construct the packet and write it to fd */
}
int rqst_on(int fd, const void *payload, size_t size,
void *answer, size_t anssize) {
/* Parse the request and prepare the answer payload
* in answer buffer (maximum size is anssize) */
...
}
How big should be answer[]? Why protocol_x() function should care
about the length of the answer that appears a request problem?
At the moment 100 could be sufficient, but tomorrow? When I'll add
a new request, I'll write a new request handler function and I
would need a bigger answer buffer.
The second is to allocate the answer[] buffer inside request
handler function:
int protocol_A(int fd, const void *buffer, size_t size) {
...
rqst_on(fd, payload, size, answer_A);
}
void answer_A(int fd, const void *payload, size_t size) {
/* Create the answer for protocol A and write it to fd */
...
}
int rqst_on(int fd, const void *payload, size_t size) {
unsigned char answer[32];
...
answer_A(fd, answer, sizeof(answer));
}
Inside the request handler function I know for sure the exact
answer length and I won't risk to add a new request that needs
a bigger answer buffer.
....but the mechanism is more complex and I need a new answer_x()
function.
What do you prefer? Do you have other better alternatives?
I can use only static allocation.
I'm writing two half-duplex protocols (protocol A and protocol B)
that will be implemented on several physical channels (at the moment,
two serial ports). Both protocols are of "slave" type: the program
waits for a request, processes it and answers. The payload (of request
and answer) should be packed in a different packet structure for protocol
A and B, but the payload are the same.
So I have one main function for each protocol:
int protocol_A(int fd, const void *buffer, size_t size);
int protocol_B(int fd, const void *buffer, size_t size);
And I have one function for each request:
int rqst_on(int fd, const void *payload, size_t size);
int rqst_off(int fd, const void *payload, size_t size);
...
fd is the file descriptor where the request was received and where
to write the answer (if any).
protocol_A() and protocol_B() parse the receiving buffer and call
the right request handler, passing the payload and its size.
Now the problem is how to handle the answer that should be packed
differently for each protocol (and I don't want to write two
request handlers, one for A and one for B).
I have two solutions, but both have some bad things.
The first is to allocate the answer buffer inside protocol_x():
int protocol_A(int fd, const void *buffer, size_t size) {
unsigned char answer[100];
size_t size = sizeof(answer);
...
n = rqst_on(fd, payload, size, answer, anssize);
/* Construct the packet and write it to fd */
}
int rqst_on(int fd, const void *payload, size_t size,
void *answer, size_t anssize) {
/* Parse the request and prepare the answer payload
* in answer buffer (maximum size is anssize) */
...
}
How big should be answer[]? Why protocol_x() function should care
about the length of the answer that appears a request problem?
At the moment 100 could be sufficient, but tomorrow? When I'll add
a new request, I'll write a new request handler function and I
would need a bigger answer buffer.
The second is to allocate the answer[] buffer inside request
handler function:
int protocol_A(int fd, const void *buffer, size_t size) {
...
rqst_on(fd, payload, size, answer_A);
}
void answer_A(int fd, const void *payload, size_t size) {
/* Create the answer for protocol A and write it to fd */
...
}
int rqst_on(int fd, const void *payload, size_t size) {
unsigned char answer[32];
...
answer_A(fd, answer, sizeof(answer));
}
Inside the request handler function I know for sure the exact
answer length and I won't risk to add a new request that needs
a bigger answer buffer.
....but the mechanism is more complex and I need a new answer_x()
function.
What do you prefer? Do you have other better alternatives?