2 * arch/arm/common/fiq_debugger_ringbuf.c
4 * simple lockless ringbuffer
6 * Copyright (C) 2010 Google, Inc.
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
21 struct fiq_debugger_ringbuf
{
29 static inline struct fiq_debugger_ringbuf
*fiq_debugger_ringbuf_alloc(int len
)
31 struct fiq_debugger_ringbuf
*rbuf
;
33 rbuf
= kzalloc(sizeof(*rbuf
) + len
, GFP_KERNEL
);
45 static inline void fiq_debugger_ringbuf_free(struct fiq_debugger_ringbuf
*rbuf
)
50 static inline int fiq_debugger_ringbuf_level(struct fiq_debugger_ringbuf
*rbuf
)
52 int level
= rbuf
->head
- rbuf
->tail
;
55 level
= rbuf
->len
+ level
;
60 static inline int fiq_debugger_ringbuf_room(struct fiq_debugger_ringbuf
*rbuf
)
62 return rbuf
->len
- fiq_debugger_ringbuf_level(rbuf
) - 1;
66 fiq_debugger_ringbuf_peek(struct fiq_debugger_ringbuf
*rbuf
, int i
)
68 return rbuf
->buf
[(rbuf
->tail
+ i
) % rbuf
->len
];
72 fiq_debugger_ringbuf_consume(struct fiq_debugger_ringbuf
*rbuf
, int count
)
74 count
= min(count
, fiq_debugger_ringbuf_level(rbuf
));
76 rbuf
->tail
= (rbuf
->tail
+ count
) % rbuf
->len
;
83 fiq_debugger_ringbuf_push(struct fiq_debugger_ringbuf
*rbuf
, u8 datum
)
85 if (fiq_debugger_ringbuf_room(rbuf
) == 0)
88 rbuf
->buf
[rbuf
->head
] = datum
;
90 rbuf
->head
= (rbuf
->head
+ 1) % rbuf
->len
;