From 13fa6bbe7bd03f8fa3b7b870bc69e67eabaa6eeb Mon Sep 17 00:00:00 2001
From: "csander@caltech.edu" <csander@caltech.edu>
Date: Tue, 27 Oct 2020 22:49:08 +0000
Subject: [PATCH] Add clang-format

---
 .clang-format        | 19 +++++++++++++++++++
 .githooks/pre-commit | 12 ++++++++++++
 src/mm-explicit.c    | 11 +++--------
 src/mm-implicit.c    | 11 +++--------
 4 files changed, 37 insertions(+), 16 deletions(-)
 create mode 100644 .clang-format
 create mode 100755 .githooks/pre-commit

diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..6facf9c
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,19 @@
+---
+# We'll use defaults from the Google style, but with 4 columns indentation.
+BasedOnStyle: Google
+# Never allow single-line functions
+AllowShortFunctionsOnASingleLine: None
+# Put "else" on a new line
+BreakBeforeBraces: Custom
+BraceWrapping:
+    BeforeElse: true
+# Allow lines up to 90 characters
+ColumnLimit: 90
+# Indent each block by 4 spaces
+IndentWidth: 4
+TabWidth: 4
+# Require 1 space before a comment on the same line
+SpacesBeforeTrailingComments: 1
+# Put a space after a cast, e.g. `(void) arg;`
+SpaceAfterCStyleCast: true
+---
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
new file mode 100755
index 0000000..dd54775
--- /dev/null
+++ b/.githooks/pre-commit
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+if loc="$(type -p "clang-format")" && [[ -x $loc ]]; then
+    for name in $(git diff --cached --name-only --diff-filter=ACM); do
+        if echo $name | grep -Eq "\.[ch]$"; then
+            echo "Formatting ${name}..."
+            clang-format -i $name
+        fi
+    done
+else
+    echo "You do not have clang-format installed; so, we were unable to unify the formatting in your files."
+fi
diff --git a/src/mm-explicit.c b/src/mm-explicit.c
index 82010aa..9623294 100644
--- a/src/mm-explicit.c
+++ b/src/mm-explicit.c
@@ -6,8 +6,8 @@
 
 #include <stdint.h>
 
-#include "mm.h"
 #include "memlib.h"
+#include "mm.h"
 
 /** The required alignment of heap payloads */
 const size_t ALIGNMENT = 2 * sizeof(size_t);
@@ -54,11 +54,8 @@ static bool is_allocated(block_t *block) {
  */
 static block_t *find_fit(size_t size) {
     // Traverse the blocks in the heap using the implicit list
-    for (
-        block_t *curr = mm_heap_first;
-        mm_heap_last != NULL && curr <= mm_heap_last;
-        curr = (void *) curr + get_size(curr)
-    ) {
+    for (block_t *curr = mm_heap_first; mm_heap_last != NULL && curr <= mm_heap_last;
+         curr = (void *) curr + get_size(curr)) {
         // If the block is free and large enough for the allocation, return it
         if (!is_allocated(curr) && get_size(curr) >= size) {
             return curr;
@@ -72,7 +69,6 @@ static block_t *block_from_payload(void *ptr) {
     return ptr - offsetof(block_t, payload);
 }
 
-
 /**
  * mm_init - Initializes the allocator state
  */
@@ -157,5 +153,4 @@ void *mm_calloc(size_t nmemb, size_t size) {
  * mm_checkheap - So simple, it doesn't need a checker!
  */
 void mm_checkheap(void) {
-
 }
diff --git a/src/mm-implicit.c b/src/mm-implicit.c
index cdd7763..1e0d039 100644
--- a/src/mm-implicit.c
+++ b/src/mm-implicit.c
@@ -7,8 +7,8 @@
 
 #include <stdint.h>
 
-#include "mm.h"
 #include "memlib.h"
+#include "mm.h"
 
 /** The required alignment of heap payloads */
 const size_t ALIGNMENT = 2 * sizeof(size_t);
@@ -55,11 +55,8 @@ static bool is_allocated(block_t *block) {
  */
 static block_t *find_fit(size_t size) {
     // Traverse the blocks in the heap using the implicit list
-    for (
-        block_t *curr = mm_heap_first;
-        mm_heap_last != NULL && curr <= mm_heap_last;
-        curr = (void *) curr + get_size(curr)
-    ) {
+    for (block_t *curr = mm_heap_first; mm_heap_last != NULL && curr <= mm_heap_last;
+         curr = (void *) curr + get_size(curr)) {
         // If the block is free and large enough for the allocation, return it
         if (!is_allocated(curr) && get_size(curr) >= size) {
             return curr;
@@ -73,7 +70,6 @@ static block_t *block_from_payload(void *ptr) {
     return ptr - offsetof(block_t, payload);
 }
 
-
 /**
  * mm_init - Initializes the allocator state
  */
@@ -158,5 +154,4 @@ void *mm_calloc(size_t nmemb, size_t size) {
  * mm_checkheap - So simple, it doesn't need a checker!
  */
 void mm_checkheap(void) {
-
 }
-- 
GitLab