4 * Copyright (C) 2004-2011 Simon Wunderlich <dotslash@packetmixer.de>
6 * This file is part of s3d, a 3d network display server.
7 * See http://s3d.berlios.de/ for more updates.
9 * s3d is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * s3d is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with s3d; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <stdio.h> /* printf(), getchar() */
26 #include <stdint.h> /* uint32_t */
27 #include <string.h> /* memcpy() */
29 int shm_write(struct buf_t
*rb
, char *buf
, int n
)
39 data
= ((char *)rb
) + sizeof(struct buf_t
);
43 if ((((s
+ size
* (1 - wrap
)) - e
) < (n
+ 1))) { /* checking free space */
44 printf("buffer reached maxsize, no resizing possible");
48 memcpy(data
+ e
, buf
, rs
); /* copy the first part ... */
49 memcpy(data
, buf
+ rs
, n
- rs
); /* .. end the rest */
51 memcpy(data
+ e
, buf
, n
); /* plain copy */
53 rb
->end
= e
+ n
; /* update end of the buffer */
54 if (rb
->end
>= rb
->bufsize
)
55 rb
->end
-= rb
->bufsize
;
59 int shm_read(struct buf_t
*rb
, char *buf
, int n
)
70 data
= ((char *)rb
) + sizeof(struct buf_t
);
73 rs
= (e
+ wrap
* size
- s
);
74 mn
= (n
> rs
) ? rs
: n
;
75 if ((wrap
) && (mn
> (size
- s
))) {
76 rs
= size
- s
; /* size of the first part */
77 memcpy(buf
, data
+ s
, rs
);
78 memcpy(buf
+ rs
, data
, mn
- rs
);
79 } else { /* no wrap (needed) */
80 memcpy(buf
, data
+ s
, mn
);
83 if (rb
->start
>= rb
->bufsize
)
84 rb
->start
-= rb
->bufsize
;
88 void ringbuf_init(char *data
, uint32_t init_size
)
90 struct buf_t
*ringbuf
= (struct buf_t
*)data
;
93 ringbuf
->bufsize
= init_size
- RB_OVERHEAD
;