[common] add a reference counted buffer type

This allows buffers to be shared between different asynchronous operations.
Once all users no longer need the buffer, it will be freed.

The motivation for this is being able to stream Wayland clipboard data
asynchronously to multiple clients. The buffer should only be freed after
the clipboard has changed and all ongoing transfer completes.
This commit is contained in:
Quantum 2021-02-11 04:01:52 -05:00 committed by Geoffrey McRae
parent 800f063a1d
commit db16efe68b
3 changed files with 78 additions and 0 deletions

View File

@ -19,6 +19,7 @@ set(COMMON_SOURCES
src/option.c
src/framebuffer.c
src/KVMFR.c
src/countedbuffer.c
)
add_library(lg_common STATIC ${COMMON_SOURCES})

View File

@ -0,0 +1,30 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2021 Guanzhong Chen <quantum2048@gmail.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stddef.h>
struct CountedBuffer {
_Atomic(size_t) refs;
size_t size;
char data[];
};
struct CountedBuffer * countedBufferNew(size_t size);
void countedBufferAddRef(struct CountedBuffer * buffer);
void countedBufferRelease(struct CountedBuffer ** buffer);

View File

@ -0,0 +1,47 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2021 Guanzhong Chen <quantum2048@gmail.com>
https://looking-glass.hostfission.com
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common/countedbuffer.h"
#include <stdlib.h>
#include <stdatomic.h>
struct CountedBuffer * countedBufferNew(size_t size)
{
struct CountedBuffer * buffer = malloc(sizeof(struct CountedBuffer) + size);
if (!buffer)
return NULL;
atomic_init(&buffer->refs, 1);
buffer->size = size;
return buffer;
}
void countedBufferAddRef(struct CountedBuffer * buffer)
{
atomic_fetch_add(&buffer->refs, 1);
}
void countedBufferRelease(struct CountedBuffer ** buffer)
{
if (atomic_fetch_sub(&(*buffer)->refs, 1) == 1)
{
free(*buffer);
*buffer = NULL;
}
}