mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
New memory management macro 'snew_plus'.
This formalises my occasional habit of using a single malloc to make a block that contains a header structure and a data buffer that a field of the structure will point to, allowing it to be freed in one go later. Previously I had to do this by hand, losing the type-checking advantages of snew; now I've written an snew-style macro to do the job, plus an accessor macro to cleanly get the auxiliary buffer pointer afterwards, and switched existing instances of the pattern over to using that.
This commit is contained in:
parent
22d2c72101
commit
452114c3d3
15
puttymem.h
15
puttymem.h
@ -49,4 +49,19 @@ void safefree(void *);
|
||||
((type *)snrealloc(sizeof((type *)0 == (ptr)) ? (ptr) : (ptr), \
|
||||
(n), sizeof(type)))
|
||||
|
||||
/*
|
||||
* For cases where you want to allocate a struct plus a subsidiary
|
||||
* data buffer in one step, this macro lets you add a constant to the
|
||||
* amount malloced.
|
||||
*
|
||||
* Since the return value is already cast to the struct type, a
|
||||
* pointer to that many bytes of extra data can be conveniently
|
||||
* obtained by simply adding 1 to the returned pointer!
|
||||
* snew_plus_get_aux is a handy macro that does that and casts the
|
||||
* result to void *, so you can assign it straight to wherever you
|
||||
* wanted it.
|
||||
*/
|
||||
#define snew_plus(type, extra) ((type *)snmalloc(1, sizeof(type) + (extra)))
|
||||
#define snew_plus_get_aux(ptr) ((void *)((ptr) + 1))
|
||||
|
||||
#endif
|
||||
|
11
sshshare.c
11
sshshare.c
@ -932,17 +932,14 @@ static void share_closing(Plug plug, const char *error_msg, int error_code,
|
||||
static void share_xchannel_add_message(
|
||||
struct share_xchannel *xc, int type, const void *data, int len)
|
||||
{
|
||||
unsigned char *block;
|
||||
struct share_xchannel_message *msg;
|
||||
|
||||
/*
|
||||
* Be a little tricksy here by allocating a single memory block
|
||||
* containing both the 'struct share_xchannel_message' and the
|
||||
* actual data. Simplifies freeing it later.
|
||||
* Allocate the 'struct share_xchannel_message' and the actual
|
||||
* data in one unit.
|
||||
*/
|
||||
block = smalloc(sizeof(struct share_xchannel_message) + len);
|
||||
msg = (struct share_xchannel_message *)block;
|
||||
msg->data = block + sizeof(struct share_xchannel_message);
|
||||
msg = snew_plus(struct share_xchannel_message, len);
|
||||
msg->data = snew_plus_get_aux(msg);
|
||||
msg->datalen = len;
|
||||
msg->type = type;
|
||||
memcpy(msg->data, data, len);
|
||||
|
@ -258,7 +258,6 @@ struct xlfd_decomposed {
|
||||
|
||||
static struct xlfd_decomposed *xlfd_decompose(const char *xlfd)
|
||||
{
|
||||
void *mem;
|
||||
char *p, *components[14];
|
||||
struct xlfd_decomposed *dec;
|
||||
int i;
|
||||
@ -266,15 +265,14 @@ static struct xlfd_decomposed *xlfd_decompose(const char *xlfd)
|
||||
if (!xlfd)
|
||||
return NULL;
|
||||
|
||||
mem = smalloc(sizeof(struct xlfd_decomposed) + strlen(xlfd) + 1);
|
||||
p = ((char *)mem) + sizeof(struct xlfd_decomposed);
|
||||
dec = snew_plus(struct xlfd_decomposed, strlen(xlfd) + 1);
|
||||
p = snew_plus_get_aux(dec);
|
||||
strcpy(p, xlfd);
|
||||
dec = (struct xlfd_decomposed *)mem;
|
||||
|
||||
for (i = 0; i < 14; i++) {
|
||||
if (*p != '-') {
|
||||
/* Malformed XLFD: not enough '-' */
|
||||
sfree(mem);
|
||||
sfree(dec);
|
||||
return NULL;
|
||||
}
|
||||
*p++ = '\0';
|
||||
@ -283,7 +281,7 @@ static struct xlfd_decomposed *xlfd_decompose(const char *xlfd)
|
||||
}
|
||||
if (*p) {
|
||||
/* Malformed XLFD: too many '-' */
|
||||
sfree(mem);
|
||||
sfree(dec);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user