理解 Memory barrier(内存屏障)【转】 (6)

fifo->in = fifo->out = 0;

fifo->lock = lock;

 

return fifo;

}

EXPORT_SYMBOL(kfifo_init);

 

/**

* kfifo_alloc - allocates a new FIFO and its internal buffer

* @size: the size of the internal buffer to be allocated.

* @gfp_mask: get_free_pages mask, passed to kmalloc()

* @lock: the lock to be used to protect the fifo buffer

*

* The size will be rounded-up to a power of 2.

*/

struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock)

{

unsigned char *buffer;

struct kfifo *ret;

 

/*

* round up to the next power of 2, since our \'let the indices

* wrap\' technique works only in this case.

*/

if (!is_power_of_2(size)) {

BUG_ON(size > 0x80000000);

size = roundup_pow_of_two(size);

}

 

buffer = kmalloc(size, gfp_mask);

if (!buffer)

return ERR_PTR(-ENOMEM);

 

ret = kfifo_init(buffer, size, gfp_mask, lock);

 

if (IS_ERR(ret))

kfree(buffer);

 

return ret;

}

EXPORT_SYMBOL(kfifo_alloc);

 

/**

* kfifo_free - frees the FIFO

* @fifo: the fifo to be freed.

*/

void kfifo_free(struct kfifo *fifo)

{

kfree(fifo->buffer);

kfree(fifo);

}

EXPORT_SYMBOL(kfifo_free);

 

/**

* __kfifo_put - puts some data into the FIFO, no locking version

* @fifo: the fifo to be used.

* @buffer: the data to be added.

* @len: the length of the data to be added.

*

* This function copies at most @len bytes from the @buffer into

* the FIFO depending on the free space, and returns the number of

* bytes copied.

*

* Note that with only one concurrent reader and one concurrent

* writer, you don\'t need extra locking to use these functions.

*/

unsigned int __kfifo_put(struct kfifo *fifo,

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsdzs.html