correct circular buffer API usage











up vote
1
down vote

favorite












I need to correct circ_buf.c which gives out these long errors ?



The most common error is error: invalid type argument of unary ‘*’ (have ‘int’)



For more context, please see this reddit thread



/*
* Filename: circ_buf.c
* Version: 1.0
* Description: A circular buffer using API from
* https://github.com/torvalds/linux/blob/master/Documentation/core-api/circular-buffers.rst
*/

#include <linux/slab.h>
#include <linux/circ_buf.h>
#include "circ_queue.h"

#define DEBUG 1

#ifdef DEBUG
#define DEBUG_MSG(...) printk(__VA_ARGS__)
#else
#define DEBUG_MSG(...)
#endif

struct item {
unsigned int val1; // event number
unsigned int val2; // relevant value tied to the event number
};

struct circ_buf * init_circ_queue(int len)
{
struct circ_buf * q;
spinlock_t head_tail_lock;

q = kzalloc(sizeof(struct circ_buf), GFP_KERNEL);
if (q == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue");
return NULL;
}

spin_lock(&head_tail_lock);

q->head = 0;
q->tail = 0;

spin_unlock(&head_tail_lock);

// creates an array of length 'len' where each array location can store a struct * item
q->buf = (char *) kzalloc(len*sizeof(struct item)*sizeof(unsigned int), GFP_KERNEL);

if (q->buf == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue array");
return NULL;
}

return q;
}

inline int push_circ_queue(struct circ_buf * buffer, unsigned int val1, unsigned int val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t producer_lock;

spin_lock(&producer_lock);

head = buffer->head;
/* The spin_unlock() and next spin_lock() provide needed ordering. */
tail = READ_ONCE(buffer->tail);

if (CIRC_SPACE(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* insert one item into the buffer */
item->val1 = val1;
item->val2 = val2;
memcpy(&buffer[head], &item, 1);

smp_store_release(buffer->head,
(head + 1) & (CIRC_BUFF_SIZE - 1));

/* wake_up() will make sure that the head is committed before
* waking anyone up */
//wake_up(consumer);

spin_unlock(&producer_lock);

return 0;
}

else
{
spin_unlock(&producer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

inline int pop_circ_queue(struct circ_buf * buffer, unsigned int * val1, unsigned int * val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t consumer_lock;

spin_lock(&consumer_lock);

/* Read index before reading contents at that index. */
head = smp_load_acquire(buffer->head);
tail = buffer->tail;

if (CIRC_CNT(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* extract one item from the buffer */
memcpy(&item, &buffer[tail], 1);
val1 = &item->val1;
val2 = &item->val2;

/* Finish reading descriptor before incrementing tail. */
smp_store_release(buffer->tail,
(tail + 1) & (CIRC_BUFF_SIZE - 1));

spin_unlock(&consumer_lock);

return 0;
}

else
{
spin_unlock(&consumer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

void free_circ_queue(struct circ_buf * q)
{
if (q == NULL)
return;

kfree(q->buf);
kfree(q);
}









share|improve this question













migrated from unix.stackexchange.com yesterday


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.















  • Maybe you want to post is to stackoverflow?
    – Sourav Ghosh
    yesterday















up vote
1
down vote

favorite












I need to correct circ_buf.c which gives out these long errors ?



The most common error is error: invalid type argument of unary ‘*’ (have ‘int’)



For more context, please see this reddit thread



/*
* Filename: circ_buf.c
* Version: 1.0
* Description: A circular buffer using API from
* https://github.com/torvalds/linux/blob/master/Documentation/core-api/circular-buffers.rst
*/

#include <linux/slab.h>
#include <linux/circ_buf.h>
#include "circ_queue.h"

#define DEBUG 1

#ifdef DEBUG
#define DEBUG_MSG(...) printk(__VA_ARGS__)
#else
#define DEBUG_MSG(...)
#endif

struct item {
unsigned int val1; // event number
unsigned int val2; // relevant value tied to the event number
};

struct circ_buf * init_circ_queue(int len)
{
struct circ_buf * q;
spinlock_t head_tail_lock;

q = kzalloc(sizeof(struct circ_buf), GFP_KERNEL);
if (q == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue");
return NULL;
}

spin_lock(&head_tail_lock);

q->head = 0;
q->tail = 0;

spin_unlock(&head_tail_lock);

// creates an array of length 'len' where each array location can store a struct * item
q->buf = (char *) kzalloc(len*sizeof(struct item)*sizeof(unsigned int), GFP_KERNEL);

if (q->buf == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue array");
return NULL;
}

return q;
}

inline int push_circ_queue(struct circ_buf * buffer, unsigned int val1, unsigned int val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t producer_lock;

spin_lock(&producer_lock);

head = buffer->head;
/* The spin_unlock() and next spin_lock() provide needed ordering. */
tail = READ_ONCE(buffer->tail);

if (CIRC_SPACE(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* insert one item into the buffer */
item->val1 = val1;
item->val2 = val2;
memcpy(&buffer[head], &item, 1);

smp_store_release(buffer->head,
(head + 1) & (CIRC_BUFF_SIZE - 1));

/* wake_up() will make sure that the head is committed before
* waking anyone up */
//wake_up(consumer);

spin_unlock(&producer_lock);

return 0;
}

else
{
spin_unlock(&producer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

inline int pop_circ_queue(struct circ_buf * buffer, unsigned int * val1, unsigned int * val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t consumer_lock;

spin_lock(&consumer_lock);

/* Read index before reading contents at that index. */
head = smp_load_acquire(buffer->head);
tail = buffer->tail;

if (CIRC_CNT(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* extract one item from the buffer */
memcpy(&item, &buffer[tail], 1);
val1 = &item->val1;
val2 = &item->val2;

/* Finish reading descriptor before incrementing tail. */
smp_store_release(buffer->tail,
(tail + 1) & (CIRC_BUFF_SIZE - 1));

spin_unlock(&consumer_lock);

return 0;
}

else
{
spin_unlock(&consumer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

void free_circ_queue(struct circ_buf * q)
{
if (q == NULL)
return;

kfree(q->buf);
kfree(q);
}









share|improve this question













migrated from unix.stackexchange.com yesterday


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.















  • Maybe you want to post is to stackoverflow?
    – Sourav Ghosh
    yesterday













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I need to correct circ_buf.c which gives out these long errors ?



The most common error is error: invalid type argument of unary ‘*’ (have ‘int’)



For more context, please see this reddit thread



/*
* Filename: circ_buf.c
* Version: 1.0
* Description: A circular buffer using API from
* https://github.com/torvalds/linux/blob/master/Documentation/core-api/circular-buffers.rst
*/

#include <linux/slab.h>
#include <linux/circ_buf.h>
#include "circ_queue.h"

#define DEBUG 1

#ifdef DEBUG
#define DEBUG_MSG(...) printk(__VA_ARGS__)
#else
#define DEBUG_MSG(...)
#endif

struct item {
unsigned int val1; // event number
unsigned int val2; // relevant value tied to the event number
};

struct circ_buf * init_circ_queue(int len)
{
struct circ_buf * q;
spinlock_t head_tail_lock;

q = kzalloc(sizeof(struct circ_buf), GFP_KERNEL);
if (q == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue");
return NULL;
}

spin_lock(&head_tail_lock);

q->head = 0;
q->tail = 0;

spin_unlock(&head_tail_lock);

// creates an array of length 'len' where each array location can store a struct * item
q->buf = (char *) kzalloc(len*sizeof(struct item)*sizeof(unsigned int), GFP_KERNEL);

if (q->buf == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue array");
return NULL;
}

return q;
}

inline int push_circ_queue(struct circ_buf * buffer, unsigned int val1, unsigned int val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t producer_lock;

spin_lock(&producer_lock);

head = buffer->head;
/* The spin_unlock() and next spin_lock() provide needed ordering. */
tail = READ_ONCE(buffer->tail);

if (CIRC_SPACE(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* insert one item into the buffer */
item->val1 = val1;
item->val2 = val2;
memcpy(&buffer[head], &item, 1);

smp_store_release(buffer->head,
(head + 1) & (CIRC_BUFF_SIZE - 1));

/* wake_up() will make sure that the head is committed before
* waking anyone up */
//wake_up(consumer);

spin_unlock(&producer_lock);

return 0;
}

else
{
spin_unlock(&producer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

inline int pop_circ_queue(struct circ_buf * buffer, unsigned int * val1, unsigned int * val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t consumer_lock;

spin_lock(&consumer_lock);

/* Read index before reading contents at that index. */
head = smp_load_acquire(buffer->head);
tail = buffer->tail;

if (CIRC_CNT(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* extract one item from the buffer */
memcpy(&item, &buffer[tail], 1);
val1 = &item->val1;
val2 = &item->val2;

/* Finish reading descriptor before incrementing tail. */
smp_store_release(buffer->tail,
(tail + 1) & (CIRC_BUFF_SIZE - 1));

spin_unlock(&consumer_lock);

return 0;
}

else
{
spin_unlock(&consumer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

void free_circ_queue(struct circ_buf * q)
{
if (q == NULL)
return;

kfree(q->buf);
kfree(q);
}









share|improve this question













I need to correct circ_buf.c which gives out these long errors ?



The most common error is error: invalid type argument of unary ‘*’ (have ‘int’)



For more context, please see this reddit thread



/*
* Filename: circ_buf.c
* Version: 1.0
* Description: A circular buffer using API from
* https://github.com/torvalds/linux/blob/master/Documentation/core-api/circular-buffers.rst
*/

#include <linux/slab.h>
#include <linux/circ_buf.h>
#include "circ_queue.h"

#define DEBUG 1

#ifdef DEBUG
#define DEBUG_MSG(...) printk(__VA_ARGS__)
#else
#define DEBUG_MSG(...)
#endif

struct item {
unsigned int val1; // event number
unsigned int val2; // relevant value tied to the event number
};

struct circ_buf * init_circ_queue(int len)
{
struct circ_buf * q;
spinlock_t head_tail_lock;

q = kzalloc(sizeof(struct circ_buf), GFP_KERNEL);
if (q == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue");
return NULL;
}

spin_lock(&head_tail_lock);

q->head = 0;
q->tail = 0;

spin_unlock(&head_tail_lock);

// creates an array of length 'len' where each array location can store a struct * item
q->buf = (char *) kzalloc(len*sizeof(struct item)*sizeof(unsigned int), GFP_KERNEL);

if (q->buf == NULL) {
DEBUG_MSG(KERN_ERR "Not enough memory to allocate circ_queue array");
return NULL;
}

return q;
}

inline int push_circ_queue(struct circ_buf * buffer, unsigned int val1, unsigned int val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t producer_lock;

spin_lock(&producer_lock);

head = buffer->head;
/* The spin_unlock() and next spin_lock() provide needed ordering. */
tail = READ_ONCE(buffer->tail);

if (CIRC_SPACE(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* insert one item into the buffer */
item->val1 = val1;
item->val2 = val2;
memcpy(&buffer[head], &item, 1);

smp_store_release(buffer->head,
(head + 1) & (CIRC_BUFF_SIZE - 1));

/* wake_up() will make sure that the head is committed before
* waking anyone up */
//wake_up(consumer);

spin_unlock(&producer_lock);

return 0;
}

else
{
spin_unlock(&producer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

inline int pop_circ_queue(struct circ_buf * buffer, unsigned int * val1, unsigned int * val2)
{
struct item *item;
unsigned long head;
unsigned long tail;
spinlock_t consumer_lock;

spin_lock(&consumer_lock);

/* Read index before reading contents at that index. */
head = smp_load_acquire(buffer->head);
tail = buffer->tail;

if (CIRC_CNT(head, tail, CIRC_BUFF_SIZE) >= 1)
{
/* extract one item from the buffer */
memcpy(&item, &buffer[tail], 1);
val1 = &item->val1;
val2 = &item->val2;

/* Finish reading descriptor before incrementing tail. */
smp_store_release(buffer->tail,
(tail + 1) & (CIRC_BUFF_SIZE - 1));

spin_unlock(&consumer_lock);

return 0;
}

else
{
spin_unlock(&consumer_lock); // still need to unlock even unused

return 1; // not enough buffer space
}
}

void free_circ_queue(struct circ_buf * q)
{
if (q == NULL)
return;

kfree(q->buf);
kfree(q);
}






kernel linux-kernel buffer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









kevin

238




238




migrated from unix.stackexchange.com yesterday


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.






migrated from unix.stackexchange.com yesterday


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.














  • Maybe you want to post is to stackoverflow?
    – Sourav Ghosh
    yesterday


















  • Maybe you want to post is to stackoverflow?
    – Sourav Ghosh
    yesterday
















Maybe you want to post is to stackoverflow?
– Sourav Ghosh
yesterday




Maybe you want to post is to stackoverflow?
– Sourav Ghosh
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53819301%2fcorrect-circular-buffer-api-usage%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53819301%2fcorrect-circular-buffer-api-usage%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt