Переглянути джерело

Lesson 23, step 4, size_t usage

Carlos 9 роки тому
батько
коміт
6f09492f2b
3 змінених файлів з 7 додано та 7 видалено
  1. 4 5
      23-fixes/README.md
  2. 1 1
      23-fixes/libc/mem.c
  3. 2 1
      23-fixes/libc/mem.h

+ 4 - 5
23-fixes/README.md

@@ -39,9 +39,11 @@ C99 introduces standard fixed-width data types like `uint32_t`
 We need to include `<stdint.h>` which works even in `-ffreestanding` (but requires stdlibs)
 and use those data types instead of our own, then delete them on `type.h`
 
-<stddef.h> to provide size\_t
-
+4. Improperly aligned `kmalloc`
+-------------------------------
 
+First, since `kmalloc` uses a size parameter, we'll use the correct data type `size_t` instead
+of `u32int_t`. `<stddef.h>` is required for `size_t`
 
 5. Missing functions
 --------------------
@@ -54,7 +56,4 @@ and use those data types instead of our own, then delete them on `type.h`
 7. Structs and attributes
 -------------------------
 
-8. Improperly aligned `kmalloc`
--------------------------------
-
 

+ 1 - 1
23-fixes/libc/mem.c

@@ -18,7 +18,7 @@ void memory_set(uint8_t *dest, uint8_t val, uint32_t len) {
 uint32_t free_mem_addr = 0x10000;
 /* Implementation is just a pointer to some free memory which
  * keeps growing */
-uint32_t kmalloc(uint32_t size, int align, uint32_t *phys_addr) {
+uint32_t kmalloc(size_t size, int align, uint32_t *phys_addr) {
     /* Pages are aligned to 4K, or 0x1000 */
     if (align == 1 && (free_mem_addr & 0xFFFFF000)) {
         free_mem_addr &= 0xFFFFF000;

+ 2 - 1
23-fixes/libc/mem.h

@@ -2,11 +2,12 @@
 #define MEM_H
 
 #include <stdint.h>
+#include <stddef.h>
 
 void memory_copy(uint8_t *source, uint8_t *dest, int nbytes);
 void memory_set(uint8_t *dest, uint8_t val, uint32_t len);
 
 /* At this stage there is no 'free' implemented. */
-uint32_t kmalloc(uint32_t size, int align, uint32_t *phys_addr);
+uint32_t kmalloc(size_t size, int align, uint32_t *phys_addr);
 
 #endif