Skip to content

Commit

Permalink
修正数据结构分类
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Dec 18, 2024
1 parent 4eb93bb commit 603f434
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions include/data-structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "data-structure/atomic/event.h"
#include "data-structure/atomic/queue-mt.h"
#include "data-structure/base.h"
#include "data-structure/kernel/kqueue.h"
#include "data-structure/ordered-map/slist-strptr.h"
#include "data-structure/ordered-set/array-list.h"
#include "data-structure/ordered-set/bitmap.h"
#include "data-structure/ordered-set/circular-buffer.h"
#include "data-structure/ordered-set/circular-queue.h"
#include "data-structure/ordered-set/event.h"
#include "data-structure/ordered-set/list.h"
#include "data-structure/ordered-set/mbitstream.h"
#include "data-structure/ordered-set/mstream.h"
#include "data-structure/ordered-set/queue-mt.h"
#include "data-structure/ordered-set/queue.h"
#include "data-structure/ordered-set/slist.h"
#include "data-structure/sorted-map/avltree.h"
Expand Down
File renamed without changes.
File renamed without changes.
26 changes: 15 additions & 11 deletions include/data-structure/kernel/kqueue.h
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
#pragma once
#include <define.h>

#ifdef __cplusplus
# error "内核不允许使用 C++"
#endif

// 未完成

typedef struct {
typedef struct kqueue {
void **buffer;
size_t head;
size_t tail;
size_t capacity;
size_t size;
} kqueue_t;
} *kqueue_t;

static inline void kqueue_init(kqueue_t *queue, void **buffer, size_t capacity) {
finline void kqueue_init(kqueue_t queue, void **buffer, size_t capacity) {
queue->buffer = buffer;
queue->head = 0;
queue->tail = 0;
queue->capacity = capacity;
atomic_store(&queue->size, 0);
atom_store(&queue->size, 0);
}

static inline bool kqueue_enqueue(kqueue_t *queue, void *item) {
size_t size = atomic_load(&queue->size);
finline bool kqueue_enqueue(kqueue_t queue, void *item) {
size_t size = atom_load(&queue->size);
if (size == queue->capacity) {
return false; // Queue is full
}
queue->buffer[queue->tail] = item;
queue->tail = (queue->tail + 1) % queue->capacity;
atomic_fetch_add(&queue->size, 1);
atom_add(&queue->size, 1);
return true;
}

static inline bool kqueue_dequeue(kqueue_t *queue, void **item) {
finline bool kqueue_dequeue(kqueue_t queue, void **item) {
size_t size = atomic_load(&queue->size);
if (size == 0) {
return false; // Queue is empty
}
*item = queue->buffer[queue->head];
queue->head = (queue->head + 1) % queue->capacity;
atomic_fetch_sub(&queue->size, 1);
atom_sub(&queue->size, 1);
return true;
}

static inline size_t kqueue_size(kqueue_t *queue) {
return atomic_load(&queue->size);
finline size_t kqueue_size(kqueue_t queue) {
return atom_load(&queue->size);
}

0 comments on commit 603f434

Please sign in to comment.