Commit 303dbf4664ed56420307fdb7ce563511b4b37f3f

Luke Dashjr 2012-03-18T20:09:03

Abstract add_cgpu function, to handle device id numbering and devices array

diff --git a/cgminer.c b/cgminer.c
index 11a7218..84019bb 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -4263,6 +4263,31 @@ void enable_device(struct cgpu_info *cgpu)
 #endif
 }
 
+struct _cgpu_devid_counter {
+	char name[4];
+	int lastid;
+	UT_hash_handle hh;
+};
+
+bool add_cgpu(struct cgpu_info*cgpu)
+{
+	static struct _cgpu_devid_counter *devids = NULL;
+	struct _cgpu_devid_counter *d;
+	
+	HASH_FIND_STR(devids, cgpu->api->name, d);
+	if (d)
+		cgpu->device_id = ++d->lastid;
+	else
+	{
+		d = malloc(sizeof(*d));
+		memcpy(d->name, cgpu->api->name, sizeof(d->name));
+		cgpu->device_id = d->lastid = 0;
+		HASH_ADD_STR(devids, name, d);
+	}
+	devices[total_devices++] = cgpu;
+	return true;
+}
+
 int main (int argc, char *argv[])
 {
 	struct block *block, *tmpblock;
diff --git a/driver-bitforce.c b/driver-bitforce.c
index 3a6deef..3c93c55 100644
--- a/driver-bitforce.c
+++ b/driver-bitforce.c
@@ -92,7 +92,6 @@ static void BFwrite(int fd, const void *buf, ssize_t bufLen)
 static bool bitforce_detect_one(const char *devpath)
 {
 	char pdevbuf[0x100];
-	static int i = 0;
 
 	if (total_devices == MAX_DEVICES)
 		return false;
@@ -117,14 +116,11 @@ static bool bitforce_detect_one(const char *devpath)
 	// We have a real BitForce!
 	struct cgpu_info *bitforce;
 	bitforce = calloc(1, sizeof(*bitforce));
-	devices[total_devices++] = bitforce;
 	bitforce->api = &bitforce_api;
-	bitforce->device_id = i++;
 	bitforce->device_path = strdup(devpath);
 	bitforce->deven = DEV_ENABLED;
 	bitforce->threads = 1;
-
-	return true;
+	return add_cgpu(bitforce);
 }
 
 static bool bitforce_detect_auto_udev()
diff --git a/driver-cpu.c b/driver-cpu.c
index 04d307c..69c112e 100644
--- a/driver-cpu.c
+++ b/driver-cpu.c
@@ -739,13 +739,12 @@ static void cpu_detect()
 	for (i = 0; i < opt_n_threads; ++i) {
 		struct cgpu_info *cgpu;
 
-		cgpu = devices[total_devices + i] = &cpus[i];
+		cgpu = &cpus[i];
 		cgpu->api = &cpu_api;
 		cgpu->deven = DEV_ENABLED;
-		cgpu->device_id = i;
 		cgpu->threads = 1;
+		add_cgpu(cgpu);
 	}
-	total_devices += opt_n_threads;
 }
 
 static void reinit_cpu_device(struct cgpu_info *cpu)
diff --git a/driver-icarus.c b/driver-icarus.c
index ba96b64..592500e 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -191,10 +191,9 @@ static bool icarus_detect_one(const char *devpath)
 	struct cgpu_info *icarus;
 	icarus = calloc(1, sizeof(struct cgpu_info));
 	icarus->api = &icarus_api;
-	icarus->device_id = total_devices;
 	icarus->device_path = strdup(devpath);
 	icarus->threads = 1;
-	devices[total_devices++] = icarus;
+	add_cgpu(icarus);
 
 	applog(LOG_INFO, "Found Icarus at %s, mark as %d",
 	       devpath, icarus->device_id);
diff --git a/driver-opencl.c b/driver-opencl.c
index a066611..7d8197a 100644
--- a/driver-opencl.c
+++ b/driver-opencl.c
@@ -1093,12 +1093,13 @@ static void opencl_detect()
 	for (i = 0; i < nDevs; ++i) {
 		struct cgpu_info *cgpu;
 
-		cgpu = devices[total_devices++] = &gpus[i];
+		cgpu = &gpus[i];
 		cgpu->deven = DEV_ENABLED;
 		cgpu->api = &opencl_api;
 		cgpu->device_id = i;
 		cgpu->threads = opt_g_threads;
 		cgpu->virtual_gpu = i;
+		add_cgpu(cgpu);
 	}
 
 	if (!opt_noadl)
diff --git a/miner.h b/miner.h
index aa46beb..259a85e 100644
--- a/miner.h
+++ b/miner.h
@@ -275,6 +275,8 @@ struct cgpu_info {
 	time_t last_share_pool_time;
 };
 
+extern bool add_cgpu(struct cgpu_info*);
+
 struct thread_q {
 	struct list_head	q;