Skip to content

Commit 9679c50

Browse files
committed
Refine comments
1 parent 6a13413 commit 9679c50

File tree

3 files changed

+107
-41
lines changed

3 files changed

+107
-41
lines changed

include/private/error.h

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,55 @@
11
#pragma once
22

3+
/* Centralizes all error codes used throughout the kernel. Error codes use
4+
* automatic enumeration starting from -16383 to avoid conflicts with POSIX
5+
* errno values. Each subsystem has its own logical grouping for easier
6+
* debugging and maintenance.
7+
*/
8+
39
enum {
4-
ERR_OK = 0,
5-
ERR_FAIL = -1,
6-
7-
/* Scheduler / task management */
8-
ERR_NO_TASKS = -16383,
9-
ERR_KCB_ALLOC,
10-
ERR_TCB_ALLOC,
11-
ERR_STACK_ALLOC,
12-
ERR_TASK_CANT_REMOVE,
13-
ERR_TASK_NOT_FOUND,
14-
ERR_TASK_CANT_SUSPEND,
15-
ERR_TASK_CANT_RESUME,
16-
ERR_TASK_INVALID_PRIO,
17-
ERR_TASK_INVALID_ENTRY,
18-
ERR_TASK_BUSY,
19-
ERR_NOT_OWNER,
20-
21-
/* Stack guard */
22-
ERR_STACK_CHECK,
23-
24-
/* IPC / synchronization */
25-
ERR_PIPE_ALLOC,
26-
ERR_PIPE_DEALLOC,
27-
ERR_SEM_ALLOC,
28-
ERR_SEM_DEALLOC,
29-
ERR_SEM_OPERATION,
30-
ERR_MQ_NOTEMPTY,
31-
ERR_TIMEOUT,
32-
33-
/* must remain the last entry */
34-
ERR_UNKNOWN
10+
/* Success and Generic Errors */
11+
ERR_OK = 0, /* Operation completed successfully */
12+
ERR_FAIL = -1, /* Generic failure */
13+
14+
/* Task Management and Scheduler Errors (auto-numbered from -16383) */
15+
ERR_NO_TASKS = -16383, /* No tasks available for scheduling */
16+
ERR_KCB_ALLOC, /* Kernel Control Block allocation failed */
17+
ERR_TCB_ALLOC, /* Task Control Block allocation failed */
18+
ERR_STACK_ALLOC, /* Task stack allocation failed */
19+
ERR_TASK_CANT_REMOVE, /* Task cannot be removed (e.g., self-remove) */
20+
ERR_TASK_NOT_FOUND, /* Task ID not found in system */
21+
ERR_TASK_CANT_SUSPEND, /* Task cannot be suspended */
22+
ERR_TASK_CANT_RESUME, /* Task cannot be resumed */
23+
ERR_TASK_INVALID_PRIO, /* Invalid task priority specified */
24+
ERR_TASK_INVALID_ENTRY, /* Invalid task entry point */
25+
ERR_TASK_BUSY, /* Task is busy or in wrong state */
26+
ERR_NOT_OWNER, /* Operation requires ownership */
27+
28+
/* Memory Protection Errors */
29+
ERR_STACK_CHECK, /* Stack overflow or corruption detected */
30+
31+
/* IPC and Synchronization Errors */
32+
ERR_PIPE_ALLOC, /* Pipe allocation failed */
33+
ERR_PIPE_DEALLOC, /* Pipe deallocation failed */
34+
ERR_SEM_ALLOC, /* Semaphore allocation failed */
35+
ERR_SEM_DEALLOC, /* Semaphore deallocation failed */
36+
ERR_SEM_OPERATION, /* Semaphore operation failed */
37+
ERR_MQ_NOTEMPTY, /* Message queue is not empty */
38+
ERR_TIMEOUT, /* Operation timed out */
39+
40+
/* Sentinel - must remain the last entry */
41+
ERR_UNKNOWN /* Unknown or unclassified error */
3542
};
3643

44+
/* Error Code Description Structure
45+
*
46+
* Maps error codes to human-readable descriptions for debugging
47+
* and error reporting purposes.
48+
*/
3749
struct error_code {
38-
int32_t code;
39-
char *const desc;
50+
int32_t code; /* The error code value */
51+
char *const desc; /* Human-readable description */
4052
};
4153

54+
/* Global error code lookup table */
4255
extern const struct error_code *const perror;

include/private/stdio.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Lightweight stdio hook layer.
1+
/* Lightweight stdio hook layer
22
*
33
* This header defines the interface for plugging in character-based I/O
44
* routines. Kernel code uses these hooks ('_putchar', '_getchar', '_kbhit') to
@@ -9,12 +9,34 @@
99

1010
#pragma once
1111

12-
/* Hook installers: Allow registration of device-specific I/O functions. */
12+
/* Hook Installation Functions
13+
*
14+
* These functions allow registration of device-specific I/O functions at
15+
* runtime. Typically called during hardware initialization to connect the
16+
* kernel I/O system to actual hardware drivers.
17+
*/
18+
19+
/* Install stdout hook for character output */
1320
void _stdout_install(int (*hook)(int));
21+
22+
/* Install stdin hook for character input */
1423
void _stdin_install(int (*hook)(void));
24+
25+
/* Install polling hook for input readiness check */
1526
void _stdpoll_install(int (*hook)(void));
1627

17-
/* Provide a consistent interface to the installed hooks. */
18-
int _putchar(int c); /* Blocking single-byte output. */
19-
int _getchar(void); /* Blocking single-byte input (busy-wait). */
20-
int _kbhit(void); /* Non-blocking poll for input readiness */
28+
/* Hook Interface Functions
29+
*
30+
* These functions provide a consistent interface to the installed hooks,
31+
* dispatching to registered implementations or default stubs if no hooks
32+
* have been installed.
33+
*/
34+
35+
/* Blocking single-byte output */
36+
int _putchar(int c);
37+
38+
/* Blocking single-byte input (busy-wait) */
39+
int _getchar(void);
40+
41+
/* Non-blocking poll for input readiness */
42+
int _kbhit(void);

include/private/utils.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,53 @@
11
#pragma once
22

3+
/* Utility Macros and Functions
4+
*
5+
* Provides essential utility macros and inline functions used throughout
6+
* the kernel for optimization, alignment, power-of-2 operations, and
7+
* compile-time analysis hints.
8+
*/
9+
310
#include <lib/libc.h>
411

12+
/* Compiler Optimization Hints
13+
*
14+
* Branch prediction hints help the compiler and processor optimize for
15+
* the most common code paths.
16+
*/
517
#define unlikely(x) __builtin_expect(!!(x), 0)
618
#define likely(x) __builtin_expect(!!(x), 1)
719

8-
#define UNUSED __attribute__((unused))
20+
/* Compiler Attributes */
21+
#define UNUSED __attribute__((unused)) /* Suppress unused warnings */
22+
23+
/* Array and Memory Utilities */
924

25+
/* Calculate number of elements in a statically allocated array */
1026
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
1127

12-
/* Align pointer forward to the next 4-byte boundary */
28+
/* Align pointer forward to the next 4-byte boundary
29+
* Essential for maintaining alignment requirements on RISC-V
30+
*/
1331
#define ALIGN4(x) ((((uint32_t) (x) + 3u) >> 2) << 2)
1432

33+
/* Power-of-2 Utility Functions
34+
*
35+
* Efficient bit manipulation functions for power-of-2 operations,
36+
* commonly used for buffer sizing and memory allocation.
37+
*/
38+
39+
/* Check if a value is a power of 2
40+
* Uses bit trick: power of 2 has only one bit set, so (x & (x-1)) == 0
41+
*/
1542
static inline bool ispowerof2(uint32_t x)
1643
{
1744
return x && !(x & (x - 1));
1845
}
1946

47+
/* Round up to the next power of 2
48+
* Uses bit manipulation to efficiently find the next power of 2
49+
* greater than or equal to the input value
50+
*/
2051
static inline uint32_t nextpowerof2(uint32_t x)
2152
{
2253
x--;

0 commit comments

Comments
 (0)