repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/skills/c/SKILL.md

Raw

name: c description: Arena allocators, size-prefixed strings, platform abstraction, defer macros, single-header libraries, type-safe patterns. Use when: writing C code, systems programming, native tools, performance-critical applications. license: MIT

C Programming

Modern systems programming patterns emphasizing control, performance, and simplicity.

Philosophy

Semantic Compression

Write code straightforwardly first. Only abstract when you have 2+ real examples with proven patterns. Prefer bottom-up refactoring over top-down design.

Make code usable before making it reusable.

Control Over Abstraction


Naming Conventions

Namespace Prefixes

Use 1-4 character prefixes to namespace types and functions:

// Types: Prefix_PascalCase
typedef struct Arena Arena;
typedef struct OS_Handle OS_Handle;
typedef struct RDI_Header RDI_Header;

// Functions: prefix_snake_case
internal void *arena_push(Arena *arena, U64 size, U64 align, B32 zero);
internal OS_Handle os_file_open(String8 path, FileOpenMode mode);

// Macros: PREFIX_SCREAMING_SNAKE_CASE
#define ARENA_HEADER_SIZE 128

Visibility Keywords

#define internal      static  // File-local functions
#define global        static  // File-local globals
#define local_persist static  // Function-local persistence

Basic Types

Define exact-width types. Never use int, long, etc. directly:

typedef uint8_t  U8;
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
typedef int32_t  S32;
typedef int64_t  S64;
typedef uint32_t B32;  // Boolean (32-bit for performance)

Code Organization

Module Structure

module/
├── module_core.h      // Types and function declarations
├── module_core.c      // Implementation
├── module_inc.h       // Master include (includes all headers)
└── module_inc.c       // Single compilation unit (includes all .c files)

Include Guards

#ifndef MODULE_CORE_H
#define MODULE_CORE_H

// ... content ...

#endif // MODULE_CORE_H

Single Compilation Unit

For faster builds and whole-program optimization:

// module_inc.c
#include "module_core.c"
#include "module_arena.c"
#include "module_strings.c"

When to Load Sub-Skills

Task Load Skill
Memory allocation patterns /c-arena
String handling /c-strings
Platform differences /c-platform
Macro patterns, defer, X-mac /c-macros
Avoiding undefined behavior /c-ub-safety
Creating single-header libs /c-single-header

Quick Reference

Memory Sizes

#define KB(n)  (((U64)(n)) << 10)
#define MB(n)  (((U64)(n)) << 20)
#define GB(n)  (((U64)(n)) << 30)

Alignment

#define AlignPow2(x, p) (((x) + ((p) - 1)) & ~((p) - 1))

Min/Max

#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))

Assertions

#define AssertAlways(x) do { if (!(x)) { Trap(); } } while (0)

#if BUILD_DEBUG
  #define Assert(x) AssertAlways(x)
#else
  #define Assert(x) ((void)(x))
#endif

#define InvalidPath    Assert(!"Invalid Path!")
#define NotImplemented Assert(!"Not Implemented!")
Markdown source
File outline
---
name: c
description: Arena allocators, size-prefixed strings, platform abstraction, defer macros, single-header libraries, type-safe patterns. Use when: writing C code, systems programming, native tools, performance-critical applications.
  license: MIT
---

# C Programming

Modern systems programming patterns emphasizing control, performance, and
simplicity.

## Philosophy

### Semantic Compression

Write code straightforwardly first. Only abstract when you have 2+ real examples
with proven patterns. Prefer bottom-up refactoring over top-down design.

Make code usable before making it reusable.

### Control Over Abstraction

- Reject unnecessary abstraction layers
- Understand what the hardware actually does
- Measure everything, don't assume
- Prefer simple, direct code over "clean" code

---

## Naming Conventions

### Namespace Prefixes

Use 1-4 character prefixes to namespace types and functions:

```c
// Types: Prefix_PascalCase
typedef struct Arena Arena;
typedef struct OS_Handle OS_Handle;
typedef struct RDI_Header RDI_Header;

// Functions: prefix_snake_case
internal void *arena_push(Arena *arena, U64 size, U64 align, B32 zero);
internal OS_Handle os_file_open(String8 path, FileOpenMode mode);

// Macros: PREFIX_SCREAMING_SNAKE_CASE
#define ARENA_HEADER_SIZE 128
```

### Visibility Keywords

```c
#define internal      static  // File-local functions
#define global        static  // File-local globals
#define local_persist static  // Function-local persistence
```

### Basic Types

Define exact-width types. Never use `int`, `long`, etc. directly:

```c
typedef uint8_t  U8;
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
typedef int32_t  S32;
typedef int64_t  S64;
typedef uint32_t B32;  // Boolean (32-bit for performance)
```

---

## Code Organization

### Module Structure

```
module/
├── module_core.h      // Types and function declarations
├── module_core.c      // Implementation
├── module_inc.h       // Master include (includes all headers)
└── module_inc.c       // Single compilation unit (includes all .c files)
```

### Include Guards

```c
#ifndef MODULE_CORE_H
#define MODULE_CORE_H

// ... content ...

#endif // MODULE_CORE_H
```

### Single Compilation Unit

For faster builds and whole-program optimization:

```c
// module_inc.c
#include "module_core.c"
#include "module_arena.c"
#include "module_strings.c"
```

---

## When to Load Sub-Skills

| Task                         | Load Skill         |
| ---------------------------- | ------------------ |
| Memory allocation patterns   | `/c-arena`         |
| String handling              | `/c-strings`       |
| Platform differences         | `/c-platform`      |
| Macro patterns, defer, X-mac | `/c-macros`        |
| Avoiding undefined behavior  | `/c-ub-safety`     |
| Creating single-header libs  | `/c-single-header` |

---

## Quick Reference

### Memory Sizes

```c
#define KB(n)  (((U64)(n)) << 10)
#define MB(n)  (((U64)(n)) << 20)
#define GB(n)  (((U64)(n)) << 30)
```

### Alignment

```c
#define AlignPow2(x, p) (((x) + ((p) - 1)) & ~((p) - 1))
```

### Min/Max

```c
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
```

### Assertions

```c
#define AssertAlways(x) do { if (!(x)) { Trap(); } } while (0)

#if BUILD_DEBUG
  #define Assert(x) AssertAlways(x)
#else
  #define Assert(x) ((void)(x))
#endif

#define InvalidPath    Assert(!"Invalid Path!")
#define NotImplemented Assert(!"Not Implemented!")
```