📄 rtfifo.h
字号:
/* * rtfifo.h - version 1.0 * * Written by Robert Kavaler, 1998-2002 * * Copyright (C) 1998-2002, Innomedia, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */#include <rtl_sync.h>#include <linux/string.h>#include <asm/atomic.h>struct RTFIFO_STRUCT { char *base; int bufsize; int start; int end; atomic_t len; spinlock_t spinlock;};static struct RTFIFO_STRUCT *rtfifo_create(int size){ struct RTFIFO_STRUCT *f; f = kmalloc(sizeof(*f), GFP_KERNEL); if(!f) { return NULL; } f->base = kmalloc(size, GFP_KERNEL); if(!f->base) { kfree(f); return NULL; } spin_lock_init(&f->spinlock); f->bufsize = size; f->start = 0; f->end = 0; atomic_set(&f->len, 0); return f;}static voidrtfifo_destroy(struct RTFIFO_STRUCT *f){ if(f) { kfree(f->base); kfree(f); }}static int inline rtfifo_get(struct RTFIFO_STRUCT *f, void *buf, const int count, int protect){ rtl_irqstate_t interrupt_state; if(!f) { return 0; } if(protect) rtl_spin_lock_irqsave(&f->spinlock, interrupt_state); if(count > atomic_read(&f->len)) { if(protect) rtl_spin_unlock_irqrestore(&f->spinlock, interrupt_state); return 0; } memcpy(buf, f->base + f->start, count); f->start += count; if(f->start >= f->bufsize) { f->start = 0; } atomic_sub(count, &f->len); if(protect) rtl_spin_unlock_irqrestore(&f->spinlock, interrupt_state); return count;}static int inline rtfifo_put(struct RTFIFO_STRUCT *f, void *buf, const int count, int protect){ rtl_irqstate_t interrupt_state; if(!f) { return 0; } if(protect) rtl_spin_lock_irqsave(&f->spinlock, interrupt_state); memcpy(f->base + f->end, buf, count); f->end += count; if(f->end >= f->bufsize) { f->end = 0; } atomic_add(count, &f->len); if(protect) rtl_spin_unlock_irqrestore(&f->spinlock, interrupt_state); return count;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -