Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 178156
b: refs/heads/master
c: 37bdfbb
h: refs/heads/master
v: v3
  • Loading branch information
Stefani Seibold authored and Linus Torvalds committed Dec 22, 2009
1 parent 226ec84 commit 9db78db
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 9842c38e917636fa7dc6b88aff17a8f1fd7f0cc0
refs/heads/master: 37bdfbbfaab47811fcec84dff23c4e8da1a09f9e
94 changes: 92 additions & 2 deletions trunk/include/linux/kfifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,60 @@ struct kfifo {
unsigned int out; /* data is extracted from off. (out % size) */
};

/*
* Macros for declaration and initialization of the kfifo datatype
*/

/* helper macro */
#define __kfifo_initializer(s, b) \
(struct kfifo) { \
.size = s, \
.in = 0, \
.out = 0, \
.buffer = b \
}

/**
* DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
* @name: name of the declared kfifo datatype
* @size: size of the fifo buffer
*
* Note: the macro can be used inside struct or union declaration
* Note: the macro creates two objects:
* A kfifo object with the given name and a buffer for the kfifo
* object named name##kfifo_buffer
*/
#define DECLARE_KFIFO(name, size) \
union { \
struct kfifo name; \
unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
}

/**
* INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO
* @name: name of the declared kfifo datatype
* @size: size of the fifo buffer
*/
#define INIT_KFIFO(name) \
name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
sizeof(struct kfifo), name##kfifo_buffer)

/**
* DEFINE_KFIFO - macro to define and initialize a kfifo
* @name: name of the declared kfifo datatype
* @size: size of the fifo buffer
*
* Note: the macro can be used for global and local kfifo data type variables
* Note: the macro creates two objects:
* A kfifo object with the given name and a buffer for the kfifo
* object named name##kfifo_buffer
*/
#define DEFINE_KFIFO(name, size) \
unsigned char name##kfifo_buffer[size]; \
struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)

#undef __kfifo_initializer

extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
unsigned int size);
extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
Expand All @@ -70,6 +124,15 @@ static inline void kfifo_reset(struct kfifo *fifo)
fifo->in = fifo->out = 0;
}

/**
* kfifo_size - returns the size of the fifo in bytes
* @fifo: the fifo to be used.
*/
static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
{
return fifo->size;
}

/**
* kfifo_len - returns the number of used bytes in the FIFO
* @fifo: the fifo to be used.
Expand All @@ -83,6 +146,33 @@ static inline unsigned int kfifo_len(struct kfifo *fifo)
return fifo->in - out;
}

/**
* kfifo_is_empty - returns true if the fifo is empty
* @fifo: the fifo to be used.
*/
static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
{
return fifo->in == fifo->out;
}

/**
* kfifo_is_full - returns true if the fifo is full
* @fifo: the fifo to be used.
*/
static inline __must_check int kfifo_is_full(struct kfifo *fifo)
{
return kfifo_len(fifo) == kfifo_size(fifo);
}

/**
* kfifo_avail - returns the number of bytes available in the FIFO
* @fifo: the fifo to be used.
*/
static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
{
return kfifo_size(fifo) - kfifo_len(fifo);
}

/**
* kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
* @fifo: the fifo to be used.
Expand Down Expand Up @@ -133,8 +223,8 @@ static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
* optimization: if the FIFO is empty, set the indices to 0
* so we don't wrap the next time
*/
if (fifo->in == fifo->out)
fifo->in = fifo->out = 0;
if (kfifo_is_empty(fifo))
kfifo_reset(fifo);

spin_unlock_irqrestore(lock, flags);

Expand Down

0 comments on commit 9db78db

Please sign in to comment.